📄 arrow-detect.sh
字号:
#!/bin/bash# arrow-detect.sh: Detects the arrow keys, and a few more.# Thank you, Sandro Magi, for showing me how.# --------------------------------------------# Character codes generated by the keypresses.arrowup='\[A'arrowdown='\[B'arrowrt='\[C'arrowleft='\[D'insert='\[2'delete='\[3'# --------------------------------------------SUCCESS=0OTHER=65echo -n "Press a key... "# May need to also press ENTER if a key not listed above pressed.read -n3 key # Read 3 characters.echo -n "$key" | grep "$arrowup" #Check if character code detected.if [ "$?" -eq $SUCCESS ]then echo "Up-arrow key pressed." exit $SUCCESSfiecho -n "$key" | grep "$arrowdown"if [ "$?" -eq $SUCCESS ]then echo "Down-arrow key pressed." exit $SUCCESSfiecho -n "$key" | grep "$arrowrt"if [ "$?" -eq $SUCCESS ]then echo "Right-arrow key pressed." exit $SUCCESSfiecho -n "$key" | grep "$arrowleft"if [ "$?" -eq $SUCCESS ]then echo "Left-arrow key pressed." exit $SUCCESSfiecho -n "$key" | grep "$insert"if [ "$?" -eq $SUCCESS ]then echo "\"Insert\" key pressed." exit $SUCCESSfiecho -n "$key" | grep "$delete"if [ "$?" -eq $SUCCESS ]then echo "\"Delete\" key pressed." exit $SUCCESSfiecho " Some other key pressed."exit $OTHER# ========================================= ## Mark Alexander came up with a simplified#+ version of the above script (Thank you!).# It eliminates the need for grep.#!/bin/bash uparrow=$'\x1b[A' downarrow=$'\x1b[B' leftarrow=$'\x1b[D' rightarrow=$'\x1b[C' read -s -n3 -p "Hit an arrow key: " x case "$x" in $uparrow) echo "You pressed up-arrow" ;; $downarrow) echo "You pressed down-arrow" ;; $leftarrow) echo "You pressed left-arrow" ;; $rightarrow) echo "You pressed right-arrow" ;; esac# ========================================= ## Exercise:# --------# 1) Add detection of the "Home," "End," "PgUp," and "PgDn" keys.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -