Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
653 views
in Technique[技术] by (71.8m points)

c - How to get Ctrl, Shift or Alt with getch() ncurses?

How to get Ctrl, Shift or Alt with getch() ncurses ?
I cannot get it work to get Ctrl, Shift or Alt with getch() using ncurses ? Do I miss something in the man ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Amazing how sometimes the right answer gets demoted, and answers that "authoritatively" give up get promoted... With a bit of creativity, key_name actually holds the right key to figuring this out, with one caveat - that SHIFT/ALT/CTRL are pressed with other keys at the same time:

  • First, for "normal keys" such as the printable ones, you can easily detect shift because it uppercases.

  • For special keys, e.g. KEY_LEFT, you will see that the code generated when SHIFT is selected is actually KEY_SLEFT. ditto for KEY_RIGHT. Unfortunately, no such luck for KEY_UP/KEY_DOWN , which seem unfazed by SHIFT. So you can distinguish by the returned char from getch() - the KEY_S.. implies shift was pressed.

  • For ALT (what's not trapped by X or the Aqua Windowmanager, at least), keyname will convert the key to an M... something.

  • For CTRL you'll get a "^" preceding the actual key name. E.g ^R for key 18

So you can now figure out the key codes for your switch(getch) statements, etc, by a simple snippet:

ch = getch(); endwin(); printf("KEY NAME : %s - %d
", keyname(ch),ch);

and that's that. Think before definitively saying "can't". Maybe there's a way that's less obvious.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...