0

I'd like to write a shell script which shall be interactive and react on the use of cursor keys. My current approach seems a little complicated. I would determine the escape sequence for the cursor keys using tput kcud1 etc. and then use read -s -n 1 a to read byte-by-byte, append the byte to a collected string, and then compare the collected string to the determined escape sequences. If one matches, then I can react.

This is problematic because I've got no idea where an escape sequence ends. For instance, tput kcud1 (down arrow) here returns "\eOB". I would use a timeout in order to ignore unknown escape sequences (and probably start over if an escape character arrives). This all doesn't look nice to me.

Isn't there a simpler way of reacting on arrow key usage in a shell script?

General Grievance
  • 4,259
  • 21
  • 28
  • 43
Alfe
  • 52,016
  • 18
  • 95
  • 150
  • Some inspiration here http://stackoverflow.com/questions/10679188/casing-arrow-keys-in-bash – Fredrik Pihl Sep 06 '13 at 10:56
  • Yeah I found that as well during research but it has no proper answer I could accept. – Alfe Sep 06 '13 at 12:08
  • The problem I see is that we do not have a function telling us that an escape sequence is still unfinished. – Alfe Sep 06 '13 at 12:25

0 Answers0