Link to home
Start Free TrialLog in
Avatar of rstaveley
rstaveleyFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Keystrokes in a console application

Where can I find out which keyboard key combinations can be reliably be detected in a console application and which ones are more often than not intercepted by the terminal or shell?
ASKER CERTIFIED SOLUTION
Avatar of Duncan Roe
Duncan Roe
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rstaveley

ASKER

If I type...

  dumpkeys | grep ^keycode | egrep '= \w+.+$'

...I get a bunch of keycodes for the kernel meymap, which I guess have been assigned to scancodes (and multi-scancodes) for the various shift states.

Can I expect to be able to read all of these in "stty raw" mode with ncurses or is it coming in at a higher level?

If I use PuTTY, I know that there are key-combinations that key intercepted by my Windows PuTTY configuration (e.g. Ctrl+Enter for full screen), but how can I figure out which key combinations are legible to ncurses from PuTTY, because I assume that doesn't correspond to the kernel keymap.
Look at "man keymaps" for more info on the output from dumpkeys.
You will have to locate PuTTY documentation as to what keys it intercepts, possibly the text window intercepts some of them. That's a Q for the Microsoft TA.
There's no direct relationship between dumpkeys and ncurses - ncrurses only sees ASCII character sequences and is not aware of modifier keys that have no effect on these.
I had thought that editors were able to access the console at a lower level than getch(), gut I guess you are telling me that lowest level you can field in a non-root terminal is the ASCII character sequences you get from ncurses and character sequences are what you get from the terminal - not something at a lower level.

I guess you are saying that as long as the terminal can propagate the character sequence, if you know what the extent of the set is, you know what's available. So my Holy Grail is to find out what character sequences are supported by ncurses.

So ...with reference to http://www.apmaths.uwo.ca/~xli/ncurses.html#input ... assuming  keypad(stdstr, TRUE) has been called, where can I find out if (say) ALT+KEY_RIGHT can reliably be detected? [In the Linux world should I avoid calling it "ALT"?]
You can detect stuff like that from a graphics application (under X) if at the console, but not, I suspect, when going via PuTTY. There are X clients for Windows but I am not aware of free ones - I've used Exceed Hummingbird in the last year or 2. In an X window you can see all keys (test this with xev)
Neither left or right ALT keys actually generate and charcters so you might be a bit stuck there - "man terminfo" mentions smsc "Enter PC scancode mode" so maybe it can be done, but not from PuTTY I would think.
...in an ncurses application which wants to use ALT key combinations, should you use `setmetamode`to get the ALT key ("meta key"?) to be translated consistently for the terminal?
setmetamode only applies to each VT (Virtual Terminal) on the console of a Linux system. It doesn't even work in an xterm:

23:14:21$ setmetamode esc
KDGKBMETA: Invalid argument
Error reading current setting. Maybe stdin is not a VT?

You need an equivalent for your local (Windows) keyboard driver.
Does xterm not get a /dev/tty?
It hadn't occurred to me that Putty/xterm consoles wouldn't be fully fledged VTs.

I see if I...

  echo Foo | wall

...from PuTTY or xterm I get the message from /dev/pts/0 or /dev/pts/1, but if I do it from a text mode console (i.e. Ctrl+Alt+F1), I see it comes from /dev/tty1.

What makes a virtual terminal? e.g. If you connect to the serial port do you get one, or does it have to be a keyboard connection?

It makes me realise how little I know about all of this!
Fully fledged VT's are physical keyboard only. As you've already observed, telnet / xterm sessions use ptys.
/dev/ttyn ... VT
/dev/ttySn ... serial port
/dev/pty* ... virtual terminal devices, created as_needed. These are actually the master "half" of the terminal (the half you type into); there is also a slave "half" from / to which the xterm &c process reads / writes. Or perhaps the other way round - see "man ptmx"

Serial ports are thus not virtual terminals.
Sorry that last post is a bit confusing a VT (sometimes called a console) is physical keyboard of the PC only. The V for virtual is because multiple VTs refer to the same hardware when active. A pty is a virtual device (not associated with any actual hardware) which I unfortunately called a "virtual terminal device" - but no way is it a VT.
ttyS (serial devices) are real devices
Ah... thanks for that explanation, that makes perfect sense now.

Of course you need a physical keyboard to get a scan code. All you get from a serial device is a bunch of characters and of course you don't get to see the likes of keypress and key release and of course pseudo terminals will be a similar deal.

This has been very educational. I can start kidding myself that I know more about Linux than I really do again :-)