How to read keys in the console? #2333
-
I'm new to the language. I'm trying it and comparing it with V for a text-based game project. So far the syntax of Odin seems more regular to me, and some of its features make coding more intuitive, but I need to read keys in the console and I'm stuck. In V I at least managed to read UTF-8 characters this way: import readline
import term
fn utf8_keypress() rune {
mut rl := readline.Readline{}
rl.enable_raw_mode_nosig()
defer {
rl.disable_raw_mode()
}
return term.utf8_getchar() or { ` ` }
} I've searched Odin's documentation and core libraries (online and sources), but found nothing about this, except an example how to read whole lines and C's package keypress
import "core:c/libc"
import "core:fmt"
main :: proc() {
fmt.println("getchar:")
key := libc.getchar()
fmt.println(key)
} Is there any way to activate the terminal raw mode in order to read individual keypresses? Thank you. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 1 reply
-
Odin's core/vendor lib currently doesn't provide an abstraction for terminal ops. Some notes/examples: https://viewsourcecode.org/snaptoken/kilo/02.enteringRawMode.html No warranty on these, but here's some bindings I used in a personal tool for some of the OS APIs mentioned in that article: |
Beta Was this translation helpful? Give feedback.
-
I believe I have a library for this, if you are interested. But I'll need to fix the library up before I publish it on Github. |
Beta Was this translation helpful? Give feedback.
-
Krixano escribió/skribis/wrote/scrit (2023-02-19T13:40:48-0800):
I believe I have a library for this, if you are interested. But I'll
need to fix the library up before I publish it on Github.
Thank you, it would be very useful.
…--
Marcos Cruz
http://programandala.net
|
Beta Was this translation helpful? Give feedback.
-
Here is the library: https://github.com/krixano/ncure I just recently checked to make sure it runs on Linux and Windows, so it should work. It also includes an implementation of a readline alternative, although there are likely some problems with it, like no proper UTF-8 support, and therefore no support for emojis. Do feel free to modify it to add in more proper UTF-8 support (the library is licensed BSD-2-clause). The library has functions to initialize the win32 terminal api, or switch to VT100 mode in Windows, to enable/disable echo and blocking in linux (disableEcho also turns off canonical mode so input is read byte-by-byte), and has a simple getch function that just gets a character (which just wraps Odin's os.read function for Linux). I've just added a full raw mode function that will also disable various other signals (CTRL+C, CTRL+V, CTRL+Z, etc.). The function is called Hopefully this helps. |
Beta Was this translation helpful? Give feedback.
Odin's core/vendor lib currently doesn't provide an abstraction for terminal ops.
You can follow any guide written for C, or check the source / step through what V does.
Some notes/examples: https://viewsourcecode.org/snaptoken/kilo/02.enteringRawMode.html
Termios docs: https://man7.org/linux/man-pages/man3/tcflow.3.html (See "Raw mode" header)
No warranty on these, but here's some bindings I used in a personal tool for some of the OS APIs mentioned in that article:
https://gist.github.com/z64/f29dbd97fa3579896787f38c7386b184