Skip to content

Instantly share code, notes, and snippets.

@bandi13
Forked from Akselmo/pointer_barrier.sh
Last active August 11, 2023 04:53
Show Gist options
  • Save bandi13/c22fe2cf009c518ec1758dd9ad3664e5 to your computer and use it in GitHub Desktop.
Save bandi13/c22fe2cf009c518ec1758dd9ad3664e5 to your computer and use it in GitHub Desktop.
Restrict mouse to screen
#!/bin/bash
# Original from: https://gist.github.com/Akselmo/d4f35798a222b9af5d0c0f1b4021d11d
# I tightened up the while loop. I find putting a little buffer (like 20 pixels) help reliability
# Usage: ./pointer_barrier.sh [Xleft [Ytop [Xright [Ybottom]]]]
# Example: ./pointer_barrier.sh 0 0 1919 1080
# Requires: sudo apt install xdotool
borderxl=${1:-20}
borderyu=${2:-40}
borderxr=${3:-1900}
borderyd=${4:-1060}
if [ $borderxl -gt $borderxr ] || [ $borderyu -gt $borderyd ]; then
echo "Invalid border"
exit 1
elif [ "$XDG_SESSION_TYPE" == "wayland" ]; then
echo "This script is mean for X11"
exit 1
else
echo "Restricting mouse $borderxl $borderyu $borderxr $borderyd"
fi
while [ true ]; do
check=0
read xpos ypos << COMMAND
$(xdotool getmouselocation | cut -d" " -f1-2 | sed -e s/.://g)
COMMAND
if [ $xpos -gt $borderxr ]; then
check=1
xpos=$borderxr
elif [ $xpos -lt $borderxl ]; then
check=1
xpos=$borderxl
fi
if [ $ypos -gt $borderyd ]; then
check=1
ypos=$borderyd
elif [ $ypos -lt $borderyu ]; then
check=1
ypos=$borderyu
fi
if [ $check -ge "1" ]; then
xdotool mousemove $xpos $ypos
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment