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

25,910

Solution 1

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\n", keyname(ch),ch);

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

Solution 2

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

#ifndef CTRL
#define CTRL(c) ((c) & 037)
#endif

switch(getch()) {
case CTRL('r'):
    /* key ctrl-r (i.e. ^R) pressed */

Dependend on used includes CTRL may or may not already been defined in your code.

Solution 3

(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".

Solution 4

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

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

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:

  • it reports the ASCII control characters (which you can do using the iscntrl macro), and
  • makes assumptions about meta (which only are useful for xterm, of the terminals you are likely to use), and
  • offers no help for the shift modifier.

Of 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 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, e.g., Tab, Backarrow and some special
                control character cases, e.g., Control-Space to make a
                NUL.

           2    enables this feature for keys including the exceptions
                listed.

which corresponds to a control sequence, seen in 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 feature), there's no reason to use it in ncurses: it would needlessly complicate getch.

Share:
25,910
klefevre
Author by

klefevre

Yet another mobile developer

Updated on April 16, 2020

Comments

  • klefevre
    klefevre about 4 years

    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 ?

  • Adam Spiers
    Adam Spiers over 7 years
    This doesn't help with writing code which works across multiple keyboard layouts. For example Shift+2 can generate different characters depending on the layout.
  • Jayant Bhawal
    Jayant Bhawal about 7 years
    I didn't try this out, but it would be nice to know why this was downvoted.
  • creativecoding
    creativecoding almost 4 years
    I appreciate this answer. FYI and to confirm the obvious, this approach might be specific to C (as the question was tagged). I tried this in Python on Linux and Ctrl was not converted into a '^'.
  • Technologeeks
    Technologeeks almost 4 years
    That's because Python bindings "cook" the codes further.
  • Mike
    Mike about 3 years
    #1: Not so. When I hit shift-return to indicate a line break in multi-line input and not a dialog complete condition, I get '\r' in either case.
  • schneiderfelipe
    schneiderfelipe over 2 years
    KEY_SR and KEY_SF seem to result from SHIFT+UP and SHIFT+DOWN, respectively.