Advertisement
Guest User

Untitled

a guest
Sep 21st, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.09 KB | None | 0 0
  1. #!/bin/bash
  2. function keychar {
  3.     parin1=$1 #first param; abc1
  4.     parin2=$2 #second param; 0=a, 1=b, 2=c, 3=1, 4=a, ...
  5.     parin2=$((parin2)) #convert to numeric
  6.     parin1len=${#parin1} #length of parin1
  7.     parin2pos=$((parin2 % parin1len)) #position mod
  8.     char=${parin1:parin2pos:1} #char key to simulate
  9.     if [ "$parin2" -gt 0 ]; then #if same key pressed multiple times, delete previous char; write a, delete a write b, delete b write c, ...
  10.         xdotool key "BackSpace"
  11.     fi
  12.     #special cases for xdotool ( X Keysyms )
  13.     if [ "$char" = " " ]; then char="space"; fi
  14.     if [ "$char" = "." ]; then char="period"; fi
  15.     if [ "$char" = "-" ]; then char="minus"; fi
  16.     xdotool key $char
  17. }
  18. datlastkey=$(date +%s%N)
  19. strlastkey=""
  20. intkeychar=0
  21. intmsbetweenkeys=2000 #two presses of a key sooner that this makes it delete previous key and write the next one (a->b->c->1->a->...)
  22. intmousestartspeed=10 #mouse starts moving at this speed (pixels per key press)
  23. intmouseacc=10 #added to the mouse speed for each key press (while holding down key, more key presses are sent from the remote)
  24. intmousespeed=10
  25.  
  26. while read oneline
  27. do
  28.     keyline=$(echo $oneline | grep " key ")
  29.     #echo $keyline --- debugAllLines
  30.     if [ -n "$keyline" ]; then
  31.         datnow=$(date +%s%N)
  32.         datdiff=$((($datnow - $datlastkey) / 1000000)) #bla bla key pressed: previous channel (123)
  33.         strkey=$(grep -oP '(?<=sed: ).*?(?= \()' <<< "$keyline") #bla bla key pres-->sed: >>previous channel<< (<--123)
  34.         strstat=$(grep -oP '(?<=key ).*?(?=:)' <<< "$keyline") #bla bla -->key >>pressed<<:<-- previous channel (123)
  35.         strpressed=$(echo $strstat | grep "pressed")
  36.         strreleased=$(echo $strstat | grep "released")
  37.         if [ -n "$strpressed" ]; then
  38.             #echo $keyline --- debug
  39.             if [ "$strkey" = "$strlastkey" ] && [ "$datdiff" -lt "$intmsbetweenkeys" ]; then
  40.                 intkeychar=$((intkeychar + 1)) #same key pressed for a different char
  41.             else
  42.                 intkeychar=0 #different key / too far apart
  43.             fi
  44.             datlastkey=$datnow
  45.             strlastkey=$strkey
  46.             case "$strkey" in
  47.                 "1")
  48.                     xdotool key "BackSpace"
  49.                     ;;
  50.                 "2")
  51.                     keychar "abc2" intkeychar
  52.                     ;;
  53.                 "3")
  54.                     keychar "def3" intkeychar
  55.                     ;;
  56.                 "4")
  57.                     keychar "ghi4" intkeychar
  58.                     ;;
  59.                 "5")
  60.                     keychar "jkl5" intkeychar
  61.                     ;;
  62.                 "6")
  63.                     keychar "mno6" intkeychar
  64.                     ;;
  65.                 "7")
  66.                     keychar "pqrs7" intkeychar
  67.                     ;;
  68.                 "8")
  69.                     keychar "tuv8" intkeychar
  70.                     ;;
  71.                 "9")
  72.                     keychar "wxyz9" intkeychar
  73.                     ;;
  74.                 "0")
  75.                     keychar " 0.-" intkeychar
  76.                     ;;
  77.                 "previous channel")
  78.                     xdotool key "Return" #Enter
  79.                     ;;
  80.                 "channel up")
  81.                     xdotool click 4 #mouse scroll up
  82.                     ;;
  83.                 "channel down")
  84.                     xdotool click 5 #mouse scroll down
  85.                     ;;
  86.                 "channels list")
  87.                     xdotool click 3 #right mouse button click"
  88.                     ;;
  89.                 "up")
  90.                     xdotool key "Up"
  91.                     ;;
  92.                 "down")
  93.                     xdotool key "Down"
  94.                     ;;
  95.                 "left")
  96.                     xdotool key "Left"
  97.                     ;;
  98.                 "right")
  99.                     xdotool key "Right"
  100.                     ;;
  101.                 "select")
  102.                     xdotool key "Ctrl+a" #left mouse button click
  103.                     ;;
  104.                 "return")
  105.                     xdotool key "Alt_L+Left" #WWW-Back
  106.                     ;;
  107.                 "exit")
  108.                     echo Key Pressed: EXIT
  109.                     ;;
  110.                 "F2")
  111.                     chromium-browser "https://www.youtube.com" &
  112.                     ;;
  113.                 "F3")
  114.                     chromium-browser "https://www.google.com" &
  115.                     ;;
  116.                 "F4")
  117.                     echo Key Pressed: YELLOW C
  118.                     ;;
  119.                 "F1")
  120.                     chromium-browser --incognito "https://www.google.com" &
  121.                     ;;
  122.                 "rewind")
  123.                     echo Key Pressed: REWIND
  124.                     ;;
  125.                 "pause")
  126.                     echo Key Pressed: PAUSE
  127.                     ;;
  128.                 "Fast forward")
  129.                     echo Key Pressed: FAST FORWARD
  130.                     ;;
  131.                 "play")
  132.                     echo Key Pressed: PLAY
  133.                     ;;
  134.                 "stop")
  135.                     ## with my remote I only got "STOP" as key released (auto-released), not as key pressed; see below
  136.                     echo Key Pressed: STOP
  137.                     ;;
  138.                 *)
  139.                     echo Unrecognized Key Pressed: $strkey ; CEC Line: $keyline
  140.                     ;;
  141.                    
  142.             esac
  143.         fi
  144.         if [ -n "$strreleased" ]; then
  145.             #echo $keyline --- debug
  146.             case "$strkey" in
  147.                 "stop")
  148.                     echo Key Released: STOP
  149.                     ;;
  150.                 "up")
  151.                     intmousespeed=$intmousestartspeed #reset mouse speed
  152.                     ;;
  153.                 "down")
  154.                     intmousespeed=$intmousestartspeed #reset mouse speed
  155.                     ;;
  156.                 "left")
  157.                     intmousespeed=$intmousestartspeed #reset mouse speed
  158.                     ;;
  159.                 "right")
  160.                     intmousespeed=$intmousestartspeed #reset mouse speed
  161.                     ;;
  162.             esac
  163.         fi
  164.     fi
  165. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement