Nicholas
Nicholas

Reputation: 1462

NCurses with Arrow Keys Plus Modifiers

I'm having trouble detecting arrow key presses with and without the SHIFT and CTRL modifiers pressed.

I have the following test code:

WINDOW * mainwin = initscr();
keypad(mainwin, TRUE);
int c = wgetch(mainwin);

This successfully returns different values for 'c' when the arrow keys - with modifiers - are pressed, and the terminal's TERM setting is set to 'xterm'. It does not work when the terminal's TERM setting is set to 'linux'. All I get for 'c' is 27 with either shift or ctrl pressed.

I would like it to work using the linux terminal type. Any suggestions?

Upvotes: 2

Views: 2785

Answers (1)

LeoNerd
LeoNerd

Reputation: 8532

The Linux console is not able to represent modified cursor keys at all. You cannot detect the Shift, Ctrl or Alt state on this console, because it is not transmitted.

On a genuine xterm or sufficiently compatible clone (and most are these days), you can detect it, but it's an ability sufficiently new that ncurses doesn't understand it and gets confused by the extended escape sequence generated.

For that case I wrote a keyboard input handling library specifically designed to handle modern terminal abilities; among them being extended key support.

You may try using that instead of curses's getch().

Upvotes: 5

Related Questions