1

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?

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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).

1 Comment

I see, the difference between lower and upper case is 32, which is why I saw 96 instead of 64. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.