Question

How are control keys obtained from the NCurses library? I need to know if a result from getch () is a character pressed with ctrl, and what character was pressed with it. I googled "ctrl keys ncurses" and "control keys ncurses" without much results.

I know from a quick test that (at least in my case) all the CTRL characters are related to the characters by a difference of 96.

ie

akey is 97
^Akey is 1
bkey is 98
^Bkey is 2
ckey is 99
^Ckey is 3

But this doesn't seem like reliable information to write a program with. Does anyone know a way of doing this?

Was it helpful?

Solution 2

That actually is reliable (but with 64, not 96). Ctrl+A all the way through Ctrl-_ are handled properly (use of Ctrl-@ is discouraged due to other meanings of NUL).

OTHER TIPS

Beyond the straight ASCII-tests, with ncurses you can use the unctrl or keyname functions to return a string which has the relevant information parsed:

  • if the length of the returned string is one, it is a simple character

  • if the length is two, and the first character is ^, then it is a control character and the corresponding canonical pressed-with character is the second character of the string.

    Keep in mind that there may be several possibilities for pressed-with, since Shift is ignored, and there are a few special cases such as ControlSpace versus Control@ which can produce the same result.

That's with ncurses: other implementations of unctrl may return a null pointer for character codes 128-255. X/Open Curses is vague on what should be done in that case.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top