Reputation: 371
I am currently trying to do raw input in Linux (e. g. the stuff I'd normally use ncurses or similar).
I already got so far as that every single keypress is directly reported to my application and is dumped as a sequence of hex codes.
If I press the 'a' key on my keyboard, I get this output:
# hex string
00 0x61 'a'
This is pretty straight-forward. 0x61 is the hex code for an ASCII 'a' which I can, in turn, print using (char) 0x61
back to the console.
Now I'm trying to do basic line-editing. If I press the left arrow key for example using xterm
, I get this output:
# hex string
00 0x1B '\x1b'
01 0x5B '['
02 0x44 'D'
According to this website, in the section "Keypad Handling",
The codes sent by the left arrow [...] can be given as
kcub1
.
According to the termcap file, the sequence should be x1b O D
:
~ infocmp xterm | grep kcub1
kcub1=\EOD
While this happens in both xterm and termite, it does not in the default tty or urxvt.
According to the xterm faq
escape sequences [...] were chosen as "PC-style" codes (like SCO "ansi"), i.e.,
ESC [ H ESC [ F
for normal mode, and
ESC O H ESC O F
for cursor application mode
I guess I have to either set xterm to "normal mode" or replace all O's in the termcap file with "["?
What would be the idial way of reading arrow keys for supporting both xterm & non-xterm terminals?
Then, there's also the cub
capability:
cub=\E[%p1%dD
On the same page, there's also a section about "Parameterized Strings".
I guess that's what the %p1%d
in the cub capability refers to?
%p1
refers to "push first parm", %d
refers to "print pop() as in printf".
What does all this mean? What gets pushed to where? I don't call pop() at any point, what does the second option mean?
Thanks for any help in advance.
EDIT: Here is a small C program that demonstrates what I'm talking about. If I compile this using gcc kilo.c
and run it, pressing the left arrow key emits 27 91 ('[') 68 ('D')
. Note the left bracket instead of the expected O
.
Upvotes: 1
Views: 315
Reputation: 54475
Terminal descriptions' list of special keys are written with the assumption that an application has initialized the terminal for full-screen mode, using application mode for special keys rather than the initial normal mode. Command-line applications generally do not do this (they could, of course: look for smkx
and rmkx
).
Further reading:
My cursor keys do not work in the ncurses FAQ
Why can't I use the cursor keys in (whatever) shell? in the xterm FAQ
Special Keys in the xterm manual.
Upvotes: 2