- Joined
- Jul 5, 2011
- Messages
- 2
- Reaction score
- 0
- Points
- 1
Ok, I just picked up bash today, and after several hours got to a point where I can use what I know fluently. Realizing that I could probably use bash for a neat graphics script, I used WSAD to move a character around the screen. During the tests, I would sometimes furiously bang on the arrow keys. To my surprise, characters showed up. Upon experimentation, I discovered that the arrow keys DO get picked up by read. It has the format ^[[A. The letter at the end indicates which arrow was pressed. The first character, however, is an escape key (ASCII 27 or 0x1B). I then set about finding a way to read keyboard input one character at a time, and test for the escape key.
The main effect of the escape key in bash is that it hides the character in front of it. So, I exploited this like so:
The code didn't work. It returned the error:
Upon further investigation, I found that $(echo $KEY$TEST) does, in fact, return "est". However, it seems to have something invisible with it, because every comparison will fail.
So, I can test for arrows, but I need to test for the escape char, and that seems to be impossible. Any suggestions?
Any and all help is greatly appreciated!
The main effect of the escape key in bash is that it hides the character in front of it. So, I exploited this like so:
Code:
#!/bin/bash
read -n 1 -s -p "Press the up arrow." KEY
TEST=test
if [ "$(echo $KEY$TEST)" = "est" ]; then
read -n 1 -s GARBAGE
read -n 1 -s KEY
if [ "$KEY" = "A" ]; then
echo "You pressed the UP arrow."
fi
else
echo "You didn't press an arrow, did you?"
fi
The code didn't work. It returned the error:
Code:
line 4: unexpected EOF while looking for matching `''
Upon further investigation, I found that $(echo $KEY$TEST) does, in fact, return "est". However, it seems to have something invisible with it, because every comparison will fail.
So, I can test for arrows, but I need to test for the escape char, and that seems to be impossible. Any suggestions?
Any and all help is greatly appreciated!