Forums
New posts
Articles
Product Reviews
Policies
FAQ
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Menu
Log in
Register
Install the app
Install
Forums
macOS & iOS Developer Playground
macOS - Development and Darwin
Reading arrow keys in terminal (partial success!)
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="matt000r000" data-source="post: 1261955" data-attributes="member: 206904"><p>Ok, I just picked up bash <em>today</em>, 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.</p><p></p><p>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:</p><p>[CODE]</p><p>#!/bin/bash</p><p>read -n 1 -s -p "Press the up arrow." KEY</p><p>TEST=test</p><p>if [ "$(echo $KEY$TEST)" = "est" ]; then</p><p> read -n 1 -s GARBAGE</p><p> read -n 1 -s KEY</p><p> if [ "$KEY" = "A" ]; then</p><p> echo "You pressed the UP arrow."</p><p> fi</p><p>else</p><p> echo "You didn't press an arrow, did you?"</p><p>fi</p><p>[/CODE]</p><p></p><p>The code didn't work. It returned the error:</p><p>[CODE]line 4: unexpected EOF while looking for matching `''[/CODE]</p><p></p><p>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.</p><p></p><p>So, I can test for arrows, <em>but</em> I need to test for the escape char, and that seems to be impossible. Any suggestions?</p><p></p><p>Any and all help is greatly appreciated!</p></blockquote><p></p>
[QUOTE="matt000r000, post: 1261955, member: 206904"] Ok, I just picked up bash [I]today[/I], 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: [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 [/CODE] The code didn't work. It returned the error: [CODE]line 4: unexpected EOF while looking for matching `''[/CODE] 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, [I]but[/I] I need to test for the escape char, and that seems to be impossible. Any suggestions? Any and all help is greatly appreciated! [/QUOTE]
Verification
Post reply
Forums
macOS & iOS Developer Playground
macOS - Development and Darwin
Reading arrow keys in terminal (partial success!)
Top