Advertisement
metalx1000

Arrow Keys in BASH

Dec 24th, 2022
1,647
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.27 KB | None | 0 0
  1. #!/bin/bash
  2. ######################################################################
  3. #Copyright (C) 2022  Kris Occhipinti
  4. #https://filmsbykris.com
  5.  
  6. #This program is free software: you can redistribute it and/or modify
  7. #it under the terms of the GNU General Public License as published by
  8. #the Free Software Foundation version 3 of the License.
  9.  
  10. #This program is distributed in the hope that it will be useful,
  11. #but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. #GNU General Public License for more details.
  14.  
  15. #You should have received a copy of the GNU General Public License
  16. #along with this program.  If not, see <http://www.gnu.org/licenses/>.
  17. ######################################################################
  18.  
  19. #create a variable that holds an escape character
  20. char=$(printf "\u1b")
  21. while [ 1 ]
  22. do
  23.   read -rsn1 key;
  24.   [[ $key == "q" ]] && exit
  25.  
  26.   if [[ $key == $char ]]
  27.   then
  28.     read -rsn2 key
  29.     [[ $key == "[A" ]] && echo "Up"
  30.     [[ $key == "[B" ]] && echo "Down"
  31.     [[ $key == "[C" ]] && echo "Right"
  32.     [[ $key == "[D" ]] && echo "Left"
  33.   fi
  34. done
  35.  
  36. # to find the value of other keys
  37. #while [ 1 ]; do read -rsn1 key;[[ $key == $char ]] && read -rsn2 key;printf %q "$key";done
  38.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement