Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

term: add key_pressed, enable_echo #23171

Merged
merged 6 commits into from
Dec 27, 2024

Conversation

kbkpbot
Copy link
Contributor

@kbkpbot kbkpbot commented Dec 15, 2024

Feature/Enhancement Request
#21826

// enable_echo enable/disable echo input characters
pub fn enable_echo(enable bool) 
// key_pressed gives back a single character, read from the standard input.
// It returns -1 on error or no character in non-blocking mode
pub fn key_pressed(blocking bool, echo bool) int 

TODO: add tests, but I don't know how ....

Huly®: V_0.6-21607

@kbkpbot kbkpbot changed the title term: add key_pressed, echo term: add key_pressed, enable_echo Dec 15, 2024
@kbkpbot
Copy link
Contributor Author

kbkpbot commented Dec 15, 2024

Example usage:

module main

import term
import time

fn main() {
        defer {
                term.enable_echo(true)
        }
        term.enable_echo(false)
        for {
                // non-blocking mode, no echo
                x := term.key_pressed(false, false)
                if x >= 0 {
                        println(x)
                }
                time.sleep(1 * time.millisecond)
        }
}

vlib/term/term_nix.c.v Outdated Show resolved Hide resolved
@spytheman
Copy link
Member

spytheman commented Dec 15, 2024

I'm using this variation to test:

module main

import term
import time

fn main() {
	unbuffer_stdout()
	term.enable_echo(false)
	defer {
		term.enable_echo(true)
	}
	for {
		// non-blocking mode, with echo
		x := term.key_pressed(false, true)
		if x == 0 {
			break
		}
		if x > 0 {
			println(x)
		}
		time.sleep(16 * time.millisecond)
		print('\r${time.now()} | ')
	}
	println('done')
}

(pressing Ctrl-D exits the loop, without having to reset the terminal, which is needed if I stop it with Ctrl-C)

@kbkpbot
Copy link
Contributor Author

kbkpbot commented Dec 15, 2024

I'm using this variation to test:

module main

import term
import time

fn main() {
	unbuffer_stdout()
	term.enable_echo(false)
	defer {
		term.enable_echo(true)
	}
	for {
		// non-blocking mode, with echo
		x := term.key_pressed(false, true)
		if x == 0 {
			break
		}
		if x > 0 {
			println(x)
		}
		time.sleep(16 * time.millisecond)
		print('\r${time.now()} | ')
	}
	println('done')
}

(pressing Ctrl-D exits the loop, without having to reset the terminal, which is needed if I stop it with Ctrl-C)

Thanks, add it to examples

…h named fields for term.key_pressed, instead of 2 boolean arguments
@spytheman
Copy link
Member

spytheman commented Dec 15, 2024

It seems that the keycodes that are used for F1..F12 + arrows + home/end keys, are different than the ones that terminals on linux/macos use 🤔 .

@spytheman
Copy link
Member

spytheman commented Dec 15, 2024

examples/term.ui/event_viewer.v (and term.ui) already does a good job of providing an uniform API to user programs, when it comes to keyboard events/key sym codes.

Unfortunately it is more of a framework (with callbacks), not a library 🤔 .

@spytheman spytheman merged commit 5b44b67 into vlang:master Dec 27, 2024
62 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants