Skip to content

Instantly share code, notes, and snippets.

@sudofox
Created January 11, 2017 22:35
Show Gist options
  • Select an option

  • Save sudofox/07a8e057cd72a41404b5fe1c1cc27795 to your computer and use it in GitHub Desktop.

Select an option

Save sudofox/07a8e057cd72a41404b5fe1c1cc27795 to your computer and use it in GitHub Desktop.
#!/bin/bash
# prank - detects spacebar presses, selects previous word (or character group that selected by ctrl+shift+left)
# and replaces it with:
REPLACEMENT_STRING="ayy lmao "
# by Sudofox/Austin Burk
#some configuration options:
# if the random number generated between 1 and RANDOM_CEIL == TRIGGER_VALUE then the word will be replaced
RANDOM_CEIL=15;
TRIGGER_VALUE=5;
# we need xinput and xdotool to perform this prank
type xinput >/dev/null 2>&1 || { echo >&2 "Error: xinput needs to be installed for this to work."; exit 1; }
type xdotool >/dev/null 2>&1 || { echo >&2 "Error: xdotool needs to be installed for this to work."; exit 1; }
prank() {
while read -r keycode; do
if [ $keycode == 65 ] && [ $(( ( RANDOM % $RANDOM_CEIL ) + 1 )) == $TRIGGER_VALUE ]; then xdotool key --delay 0 "Ctrl+Shift+Left";
xdotool key "BackSpace"; xdotool type --delay 0 "$REPLACEMENT_STRING"
fi;done;
}
# find proper keyboard xinput id by finding the device that the kernel System Request keypress handler is bound to
# (this is the most reliable thing I was able to think of.)
kbd_eventid=$(grep sysrq /proc/bus/input/devices|grep -Po "event.*"|awk '{print $1}')
for id in $(xinput list |grep -i keyboard|grep -vi Power|grep -Po "id.*"|awk '{print $1}'|awk -F= '{print $2}'); do
matches=$(xinput list-props $id|grep $kbd_eventid|wc -l);
if [ $matches == 1 ]; then device_id=$id;break; fi;
done;
xinput test $device_id |grep --line-buffered release|awk '{print $3; system("")}'|prank
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment