How to get Ctrl, Shift or Alt using getch () ncurses?

How to get Ctrl , Shift or Alt using getch() ncurses?
Can't I make it work to get Ctrl , Shift or Alt using getch() using ncurses? Am I missing something in a person?

+6
source share
6 answers

Itโ€™s amazing how sometimes the right answer is demoted and the answers that are given โ€œauthoritativelyโ€ are promoted ... With a little creativity, the key name actually contains the right key to understand it, with one caveat - that SHIFT / ALT / CTRL are simultaneously pressed other keys:

  • Firstly, for โ€œnormal keys,โ€ such as printed keys, you can easily detect a shift, because these are top keys.

  • For special keys, for example. KEY_LEFT, you will see that the code generated when selecting SHIFT is actually KEY_SLEFT. Same thing for KEY_RIGHT. Unfortunately, there is no such luck for KEY_UP / KEY_DOWN, which do not seem to be impressed with SHIFT. Thus, you can distinguish the returned char from getch () - KEY_S .. implies a shift has been pressed.

  • For ALT (which is not captured by X or the Aqua Windowmanager, at least), keyname will convert the key to M ... something.

  • For CTRL, you get "^" preceding the actual key name. For example, ^ R for key 18

So now you can find out the key codes for switch (getch) statements, etc. simple snippet:

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

what is it. Think before you finally say "can't." Maybe there is a way that is less obvious.

+14
source

(To roughly copy my answer from How to get the Shift + X / Alt + X keys in Curses? )

In short, you cannot. Modifiers are just those modifiers. They do not exist on their own, they modify another (printed) key that you can press.

However, if you feel particularly brave, you can try my libtermkey , which will at least correctly analyze things like Ctrl -. arrow

Finally, if you feel even more brave, you can run the terminal I wrote, pangoterm , which has common ways to encode any randomly modified Unicode keys, so it can distinguish between Ctrl - m from Enter , Ctrl - Shift - a from Ctrl - a etc.

However, outside of them, the answer remains "you cannot."

+5
source

At least for the control modifier there is a simple solution. The curses were obtained from the vi source code, in which you will find the following (see https://github.com/nt-roff/ex-1.1/blob/master/ex.h line 77 and https://github.com/ nt-roff / ex-1.1 / blob / master / ex_vops.c line 445)

 #ifndef CTRL #define CTRL(c) ((c) & 037) #endif switch(getch()) { case CTRL('r'): /* key ctrl-r (ie ^R) pressed */ 

Dependence on the used includes CTRL may or may not be defined in your code.

+1
source

You cannot, there is no extension in any of the main terminal emulators to achieve it.

If you are considering the X11 environment, you can use the X11 functions to get it, but that is a completely different matter.

0
source

You can call key_name( c ) to rotate the key generated from getch() into what shows the state of the ctrl modifier.

For example, this code shows "^ R" if you press ctrl-r:

 while( true ) { char c = getch(); if ( ERR == c ) break; const char *name = key_name( c ); move( 2, 2 ); clear(); printw( "You entered: %s ", name ); refresh(); } 
0
source

Agreeing (partially) with @leonerd, ncurses will only give you these keys, as they are used as modifiers for other keys (ignoring the ASCII escape character, which some people confuse with the Alt key). Some specific devices may be asked to provide this information (for example, the Linux console, as described in console_ioctl (4) ), but this is 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 does not tell you whether this modifier was used (except in special cases where there were widely known uses of shift ), but its descriptions in terms provide information either

  • multiplying the actual function keys by a combination of shift and control modifiers or by
  • using names based on the xterm function keys for the PC (shift 2, alt is 3, 5, etc.) to provide information.

There are two approaches: the first uses an array of no more than 60 function keys (good enough for shift and control combinations), and the other just uses custom names).

All of these modified keys give several bytes; an application using keypad() (of course) in ncurses will get a single number. In the latter case, key codes are determined at runtime.

This applies mainly to special keys (function, edit, and cursor keys). For ordinary keys, we can assume that keyname gives some special behavior, but when reading a description, this is not so:

  • it reports ASCII control characters (which you can use with the iscntrl macro) and
  • makes assumptions about meta (which are only useful for xterm , the terminals that you most likely use) and
  • doesn't offer help for shift modifier.

From the terminals ... everyone has modifier information available internally, but the terminals usually do not have the ability to pass this information to applications. xterm can do this using the modifyOtherKeys resource,

  modifyOtherKeys (class ModifyOtherKeys) Like modifyCursorKeys, tells xterm to construct an escape sequence for other keys (such as "2") when modified by Control-, Alt- or Meta-modifiers. This feature does not apply to function keys and well-defined keys such as ESC or the control keys. The default is "0": 0 disables this feature. 1 enables this feature for keys except for those with well- known behavior, eg, Tab, Backarrow and some special control character cases, eg, Control-Space to make a NUL. 2 enables this feature for keys including the exceptions listed. 

which corresponds to the control sequence presented in the XTerm Control Sequences :

 CSI > Ps; Ps m Set or reset resource-values used by xterm to decide whether to construct escape sequences holding information about the modifiers pressed with a given key. The first parameter iden- tifies the resource to set/reset. The second parameter is the value to assign to the resource. If the second parameter is omitted, the resource is reset to its initial value. Ps = 0 -> modifyKeyboard. Ps = 1 -> modifyCursorKeys. Ps = 2 -> modifyFunctionKeys. Ps = 4 -> modifyOtherKeys. 

but (being an xterm-specific function), there is no reason to use it in ncurses: it would easily complicate getch .

0
source

Source: https://habr.com/ru/post/910985/


All Articles