• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 554
  • Last Modified:

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?
0
rstaveley
Asked:
rstaveley
  • 6
  • 6
1 Solution
 
Duncan RoeSoftware DeveloperCommented:
stty -a will show you special characters. These are intercepted by the console driver - the shell doesn't see anything. "stty raw" will turn them all off.
Function keys send a multi-character sequence. These can be reliably read by using the keyboard input functions in the ncurses library (aka terminfo)
0
 
rstaveleyAuthor Commented:
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.
0
 
Duncan RoeSoftware DeveloperCommented:
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.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
rstaveleyAuthor Commented:
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"?]
0
 
Duncan RoeSoftware DeveloperCommented:
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.
0
 
rstaveleyAuthor Commented:
...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?
0
 
Duncan RoeSoftware DeveloperCommented:
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.
0
 
rstaveleyAuthor Commented:
Does xterm not get a /dev/tty?
0
 
rstaveleyAuthor Commented:
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!
0
 
Duncan RoeSoftware DeveloperCommented:
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.
0
 
Duncan RoeSoftware DeveloperCommented:
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
0
 
rstaveleyAuthor Commented:
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 :-)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 6
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now