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 ?
相关问题
- Multiple sockets for clients to connect to
- What is the best way to do a search in a large fil
- glDrawElements only draws half a quad
- Index of single bit in long integer (in C) [duplic
- Equivalent of std::pair in C
At least for the control modifier there is a simple solution. Curses had been derived from vi source code, in which you find the following (see https://github.com/n-t-roff/ex-1.1/blob/master/ex.h line 77 and https://github.com/n-t-roff/ex-1.1/blob/master/ex_vops.c line 445):
Dependend on used includes
CTRL
may or may not already been defined in your code.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:
and that's that. Think before definitively saying "can't". Maybe there's a way that's less obvious.
You can't, there's no extension in any of the major terminal emulators to achieve it.
If you're assuming an X11 environment you can use X11 functions to retrieve it, but that's a different question altogether.
Agreeing (partly) with @leonerd, ncurses will only give you those keys as they are used as modifiers to other keys (ignoring the ASCII escape character which some people confuse with the Alt key). Some specific devices can be told to give this information (e.g., Linux console as documented in console_ioctl(4)), but that's not a problem that ncurses will solve for you.
Refer to the ncurses FAQ How can I use shift- or control-modifiers? for a long answer.
But short: ncurses doesn't tell you if a given modifier was used (except for special cases where there were well-known uses of shift), but rather its terminal descriptions provide the information either by
There are two approaches because the first uses an array of no more than 60 function keys (good enough for shift- and control-combinations), while the other just uses user-defined names).
All of these modified keys give multiple bytes; an application using
keypad()
(of course) in ncurses would get a single number. In the latter case, the keycodes are determined at runtime.That applies mainly to the special keys (function-, editing- and cursor-keys). For regular keys, one might assume that
keyname
gives some special behavior, but reading the description it does not:iscntrl
macro), andxterm
, of the terminals you are likely to use), andOf terminals... all have the modifier information available internally, but terminals generally do not have a way to pass this information to applications.
xterm
can do this using themodifyOtherKeys
resource,which corresponds to a control sequence, seen in XTerm Control Sequences:
but (being an xterm-specific feature), there's no reason to use it in ncurses: it would needlessly complicate
getch
.You can call
key_name( c )
to turn the key generated fromgetch()
into something that shows you the state of the ctrl-modifier.For example this code shows "^R" if you press ctrl-r:
(To roughly copy my answer from How to get Shift+X / Alt+X keys in Curses ?)
Long story short - you cannot. The modifier keys are just that - modifiers. They do not exist in their own right, they modify some other (printing) key that you might press.
That said, if you are feeling especially brave, you can try my libtermkey which will at least correctly parse things like Ctrl-arrow.
Finally if you're feeling even braver you can run the terminal I wrote, pangoterm, which has generic ways to encode any arbitrarily modified Unicode keys, so it can distinguish Ctrl-m from Enter, Ctrl-Shift-a from Ctrl-a, etc...
However, outside of these, the answer remains "you cannot".