5

I have a curses based application (WordGrinder). I've just had a bug report from a user saying that some keys aren't working properly on his keyboard. On investigation, he's right.

The keys in question are SHIFT+cursor keys and some of the keypad navigation keys, such as END. Investigating what's going on, it seems that curses is not sending me events for these keys. In the case of SHIFT+cursor keys, I'm not getting anything at all, and for END I'm getting a raw escape sequence.

This surprises me. All the other keys are getting interpreted and translated correctly into keysyms. I'd expect to be getting KEY_SLEFT and KEY_END. Why aren't I?

I've looked at some other applications where these keys work but don't spot anything obvious that I'm doing wrong; and applications like nano do really evil things like handle their own escape key parsing anyway, so I don't know if they're a good candidate for source code.

I'm initialising ncurses as follows:

initscr();
raw();
noecho();
meta(NULL, TRUE);
nonl();
idlok(stdscr, TRUE);
idcok(stdscr, TRUE);
scrollok(stdscr, FALSE);
intrflush(stdscr, FALSE);
keypad(stdscr, TRUE);

I'm using gnome-terminal as the terminal emulator, and xterm as the terminal type. Locale is UTF-8 and I've got the ncursesw variant of the library.

Any ideas?

Update:

Well, many months later I try Wordgrinder with Gnome 3's gnome-terminal and discover that all these wacky keys generate valid ncurses keycodes. For example, SHIFT+LEFT now produces keycode 393. xterm produces exactly the same result. Unfortunately, CTRL+LEFT produces keycode 539, and the Curses documentation states clearly that valid keycodes are in the range KEY_MIN to KEY_MAX --- 257 to 511...

So at least things work now, but how do these weird new keycodes work? Are they defined anywhere? They're certainly not in the headers.

David Given
  • 13,277
  • 9
  • 76
  • 123
  • Can you try running it in a different terminal (e.g. ssh to the box using osx ssh or windows putty and run the app)? Do you see the same problems? – Foo Bah Sep 04 '11 at 19:00

3 Answers3

1

gnome-terminal is not an xterm. It sends different combinations for shift-arrow and control-arrow. With ncurses version 5.5 setting TERM to gnome will probably work.

Here is some information: http://invisible-island.net/xterm/xterm.faq.html#bug_gnometerm

Craig
  • 4,750
  • 22
  • 21
  • Current Debian does not recognise `gnome` as being a terminal type. And I get identical behaviour if I test with traditional xterm (with the terminal type set to xterm) as I do in gnome-terminal. Ditto rxvt-unicode (with the terminal type set to rxvt)... – David Given Sep 28 '11 at 10:33
  • My best guess is that something is wrong with the terminfo/termcap install. I am unfamiliar with debian so I can't speak to specifics. What version of ncurses is installed? – Craig Sep 28 '11 at 13:42
0

I have done a raw [ cfmakeraw ] STDIN scan using EPOLL. And I can confirm that SHIFT+LEFT; SHIFT+RIGHT; are NOT "scanned". So how does XLib client read those keys ?

XLib client/driver uses direct keyboard drivers (using good old BIOS's multiplex Hardware INTERRUPT hooks)...There are no other ways to "scan" [Forgotten RAW] keyboard state ) :-)

NCurses is a client of the serial /dev/TTY* for the obvious networking reasons - Not a hardware hook.

Bretzelus
  • 325
  • 2
  • 8
0

There's a good chance that gnome-terminal is intercepting your SHIFT-arrow keys for its own purposes. I would suggest running your application in xterm or from the console.

Seth
  • 2,683
  • 18
  • 18
  • Nope, if I do `cat > /dev/null` I can see arrow keys and shift+arrow keys produce different escape sequences (`^[[C` and `^[[1;2C` respectively)... – David Given Jul 13 '11 at 09:40
  • OK. Second option: What type of variable are you storing the getch() returns in? Any chance you are using `char` or `short`? – Seth Jul 13 '11 at 11:47
  • I'm actually using get_wch(), as I need to be aware of Unicode, so the key type is an int. – David Given Jul 13 '11 at 14:32
  • OK, I see. I checked out the source and compiled locally (Debian Squeeze, lua 5.1.4, ncurses 5.7). I am able to use the shift-left and shift-right functionality without issue. Which versions are you using for your test? – Seth Jul 13 '11 at 18:48
  • You mean you compiled WordGrinder? If so, ta! In fact it's in Debian, and I have a stock Debian Wheezy system here with WordGrinder running in a gnome-terminal and it's *not* working. But use `cat` I can see the terminal produce different escape sequences... – David Given Jul 14 '11 at 19:33