User Tools

Site Tools


notes:python_linux_keyboard_event_injection

Python Linux Keyboard Event Injection

This script types echo hello and sends a carriage return, and then a SYN_REPORT which appears to be required to “flush” the events through:

keys.py
import os
import struct
import time
 
time.sleep(2)
 
keys = (18, 46, 35, 24, 57, 35, 18, 38, 38, 24, 28)
fd = os.open("/dev/input/event3", os.O_WRONLY)
for key in keys:
    os.write(fd, struct.pack("LLHHI", 0, 0, 1, key, 1))
    os.write(fd, struct.pack("LLHHI", 0, 0, 1, key, 0))
os.write(fd, struct.pack("LLHHI", 0, 0, 0, 0, 0))
os.close(fd)

The relevant structures and definitions came from: http://lxr.free-electrons.com/source/include/uapi/linux/input.h.

notes/python_linux_keyboard_event_injection.txt · Last modified: 2013/05/09 14:36 by andy