4-finger tap to close Firefox
A Python script that reads raw touch events and closes Firefox when 4 fingers are placed on the screen simultaneously. Runs as a systemd service.
Step 1 — Install the evdev Python library:
apt install -y python3-evdev
Step 2 — Create the gesture script:
cat > /home/kiosk/gesture-close.py << 'EOF'
#!/usr/bin/env python3
import evdev
import subprocess
import time
touch_device = None
for path in evdev.list_devices():
device = evdev.InputDevice(path)
if 'ILITEK' in device.name or 'touch' in device.name.lower():
touch_device = device
break
if not touch_device:
print("Touchscreen not found!")
exit(1)
print(f"Using device: {touch_device.name} at {touch_device.path}")
finger_count = 0
last_trigger = 0
for event in touch_device.read_loop():
if event.type == evdev.ecodes.EV_ABS:
if event.code == evdev.ecodes.ABS_MT_TRACKING_ID:
if event.value >= 0:
finger_count += 1
else:
finger_count = max(0, finger_count - 1)
if finger_count >= 4:
now = time.time()
if now - last_trigger > 2:
last_trigger = now
subprocess.run(['pkill', 'firefox'])
EOF
chmod +x /home/kiosk/gesture-close.py
chown kiosk:kiosk /home/kiosk/gesture-close.py
Step 3 — Create a systemd service:
cat > /etc/systemd/system/kiosk-gesture.service << 'EOF'
[Unit]
Description=Kiosk 4-finger gesture close
After=graphical.target
After=systemd-udev-settle.service
[Service]
ExecStartPre=/bin/sleep 10
ExecStart=/usr/bin/python3 /home/kiosk/gesture-close.py
Restart=always
RestartSec=5
User=root
[Install]
WantedBy=graphical.target
EOF
systemctl daemon-reload
systemctl enable kiosk-gesture
systemctl start kiosk-gesture
Step 4 — Add sudoers entry and autostart fallback:
echo "kiosk ALL=(root) NOPASSWD: /usr/bin/python3 /home/kiosk/gesture-close.py" >> /etc/sudoers
Add to autostart as a fallback:
sleep 20 && sudo /usr/bin/python3 /home/kiosk/gesture-close.py &
How it works: Place 4 fingers on the screen simultaneously. Firefox will close and the launch-browser.sh restart loop will reopen it after 2 seconds.
Why run as root? Firefox grabs the touch device exclusively. Running the script as root allows it to read touch events even while Firefox has focus.