Bash case with arrow keys (and DEL, BACKSPACE,etc)
I am trying to write a bash script using 'case' but I cannot figure out what the escape sequence would be for the arrow keys. Also, getting the escape sequence for the delete and backspace keys would be useful too. I know that \d is supposed to be delete, but it is not detecting properly....
|
If you don't mind me asking, is there any specific reason why you (are forced to) focus on using those keys?
|
Quote:
|
Hmm. Similar to OSD stuff, right? To start with the last, CTRL+C can't be assigned since it sends a signal. The "best" ways to handle that within a script is to use "trap" since you can assign anything to that like a function to clean up before you go. Crashing is always interesting :-] Did you try to run your script with "set -xe" below the shebang line and look at the output? As far as the keys thing is concerned there are scripts (here on LQ too IIRC) that read single char input but in what way you can use it, I don't know. The problem is you probably will have to intercept settings (maybe using a custom inputrc) to make the keys emit something you can use. To give you an idea see: http://www.ibb.net/~anne/keyboard/troubleshooting.html.
|
This is a belated response - but maybe it can help someone else searching for a solution. Here is an example how to capture most keys and map them in the same file.
#!/bin/bash4 #(i copied bashV4 from F11 since it has associative arrays) while IFS='^B' read -sn1 a # ^B is Control B do k=`perl -e "printf('%d',ord('$a'));"` if (( k == 27 )) ; then while IFS='' read -sn1 -t1 c do l=`perl -e "printf('%d',ord('$c'));"` k="$k $l" grep "^$k 0 " $0 > /dev/null if (( $? == 0 )) ; then break fi done fi echo $k grep "^$k 0 " $0 done # never gets here exit 27 79 70 0 end 27 79 72 0 home 27 79 81 0 f2 27 79 82 0 f3 27 79 83 0 f4 27 91 49 53 126 0 f5 27 91 49 55 126 0 f6 27 91 49 56 126 0 f7 27 91 49 57 126 0 f8 27 91 50 48 126 0 f9 27 91 50 49 126 0 f10 27 91 50 52 126 0 f12 27 91 50 126 0 insert 27 91 51 126 0 delete 27 91 49 126 0 home 27 91 52 126 0 end 27 91 53 126 0 pageUp 27 91 54 126 0 pageDown 27 91 65 0 up 27 91 66 0 down 27 91 67 0 right 27 91 68 0 left 27 91 49 59 53 67 0 ctl-right 27 91 49 59 53 68 0 ctl-left 27 91 49 59 53 65 0 ctl-up 27 91 49 59 53 66 0 ctl-down 27 91 69 0 keypad-five 9 0 tab 33 0 bang 35 0 pound 36 0 dollarSign 37 0 percent 38 0 ampersand 40 0 openParen |
A more complete solution
for anybody who like me ended up here looking for this solution.
this solution should manage just about any key combination. Note alot of the keys may/will be mapped to X, window manager, terminal, etc... Only real proble is detecting Escape,ctrl-[,ctrl-O as they are part of the sequences of the other keys. so for example with this implementation pressing Escape 2 within a second will return ctrl-[ instead of Escape. Code:
|
Run it and it exits without a keypress or anything. Some inherent characteristics of this site's HTML may have eaten some of the characters.
|
just call the function at the end of the file. Like this.
Code:
SpecialKeyCodes=" Enter Escape alt-A alt-a ctrl-alt-shift-left |
That's what C+ncurses is good for... (of course you can eat your breakfast with a comb, but a fork is much better.)
|
Last version no, I think unless someone finds a bug in it.
I'd have to disagree. for my purposes c and ncurses would be overkill.
I've wanted this sort of functionality in bash for a while now, so I finally got myself to do it. And while I was at it I wanted a decent solution. Anyway now fixed the last of the problems and optimized for performance. takes less than 0.005s for all keys now. most are less than 0.001s. Also reduced the footprint. The code should be easier to read now. Code:
|
Added Mouse support and Sun Function Keys
Another rewrite, as I got more comfortable with how it all works.
Added full mouse support at least as much as seems to be supported by my terminal i.e. btn 1-3 scroll up and scroll down & motion tracking. added the codes for Sun Function keys and Meta key. i.e. Ctrl Alt Shift & Meta. Code:
#!/bin/bash |
I moved this to http://mywiki.wooledge.org/ReadingFunctionKeysInBash
Also added a proper bash implemented vt100 parser example. |
All times are GMT -5. The time now is 11:28 AM. |