Debian Codegraph @

arrow-detect.sh

raw symbols SCIP
1 #!/bin/bashscip-shell filesystem . . `/bin/bash`.
2 # arrow-detect.sh: Detects the arrow keys, and a few more.
3 # Thank you, Sandro Magi, for showing me how.
4
5 # --------------------------------------------
6 # Character codes generated by the keypresses.
7 arrowup='\[A'
8 arrowdown='\[B'
9 arrowrt='\[C'
10 arrowleft='\[D'
11 insert='\[2'
12 delete='\[3'
13 # --------------------------------------------
14
15 SUCCESS=0
16 OTHER=65
17
18 echoscip-shell system . . echo. -n "Press a key... "
19 # May need to also press ENTER if a key not listed above pressed.
20 read -n3 key # Read 3 characters.
21
22 echoscip-shell system . . echo. -n "$key" | grepscip-shell system . . grep. "$arrowup" #Check if character code detected.
23 if [scip-shell system . . `[`. "$?" -eq $SUCCESS ]
24 then
25 echoscip-shell system . . echo. "Up-arrow key pressed."
26 exit $SUCCESS
27 fi
28
29 echoscip-shell system . . echo. -n "$key" | grepscip-shell system . . grep. "$arrowdown"
30 if [scip-shell system . . `[`. "$?" -eq $SUCCESS ]
31 then
32 echoscip-shell system . . echo. "Down-arrow key pressed."
33 exit $SUCCESS
34 fi
35
36 echoscip-shell system . . echo. -n "$key" | grepscip-shell system . . grep. "$arrowrt"
37 if [scip-shell system . . `[`. "$?" -eq $SUCCESS ]
38 then
39 echoscip-shell system . . echo. "Right-arrow key pressed."
40 exit $SUCCESS
41 fi
42
43 echoscip-shell system . . echo. -n "$key" | grepscip-shell system . . grep. "$arrowleft"
44 if [scip-shell system . . `[`. "$?" -eq $SUCCESS ]
45 then
46 echoscip-shell system . . echo. "Left-arrow key pressed."
47 exit $SUCCESS
48 fi
49
50 echoscip-shell system . . echo. -n "$key" | grepscip-shell system . . grep. "$insert"
51 if [scip-shell system . . `[`. "$?" -eq $SUCCESS ]
52 then
53 echoscip-shell system . . echo. "\"Insert\" key pressed."
54 exit $SUCCESS
55 fi
56
57 echoscip-shell system . . echo. -n "$key" | grepscip-shell system . . grep. "$delete"
58 if [scip-shell system . . `[`. "$?" -eq $SUCCESS ]
59 then
60 echoscip-shell system . . echo. "\"Delete\" key pressed."
61 exit $SUCCESS
62 fi
63
64
65 echoscip-shell system . . echo. " Some other key pressed."
66
67 exit $OTHER
68
69 # ========================================= #
70
71 # Mark Alexander came up with a simplified
72 #+ version of the above script (Thank you!).
73 # It eliminates the need for grep.
74
75 #!/bin/bash
76
77 uparrow=$'\x1b[A'
78 downarrow=$'\x1b[B'
79 leftarrow=$'\x1b[D'
80 rightarrow=$'\x1b[C'
81
82 read -s -n3 -p "Hit an arrow key: " x
83
84 case "$x" in
85 $uparrow)
86 echoscip-shell system . . echo. "You pressed up-arrow"
87 ;;
88 $downarrow)
89 echoscip-shell system . . echo. "You pressed down-arrow"
90 ;;
91 $leftarrow)
92 echoscip-shell system . . echo. "You pressed left-arrow"
93 ;;
94 $rightarrow)
95 echoscip-shell system . . echo. "You pressed right-arrow"
96 ;;
97 esac
98
99 exit $?
100
101 # ========================================= #
102
103 # Antonio Macchi has a simpler alternative.
104
105 #!/bin/bash
106
107 while truescip-shell system . . true.
108 do
109 read -sn1 a
110 testscip-shell system . . test. "$a" == `echoscip-shell system . . echo. -en "\e"` || continue
111 read -sn1 a
112 testscip-shell system . . test. "$a" == "[" || continue
113 read -sn1 a
114 case "$a" in
115 A) echoscip-shell system . . echo. "up";;
116 B) echoscip-shell system . . echo. "down";;
117 C) echoscip-shell system . . echo. "right";;
118 D) echoscip-shell system . . echo. "left";;
119 esac
120 done
121
122 # ========================================= #
123
124 # Exercise:
125 # --------
126 # 1) Add detection of the "Home," "End," "PgUp," and "PgDn" keys.