Skip to content

Instantly share code, notes, and snippets.

@HarlemSquirrel
Created February 5, 2020 00:20
Show Gist options
  • Save HarlemSquirrel/dbc306b9e550496e8d150992d6c6c256 to your computer and use it in GitHub Desktop.
Save HarlemSquirrel/dbc306b9e550496e8d150992d6c6c256 to your computer and use it in GitHub Desktop.
Sample test sending keys on Wayland from string to emulate typing.
import sys
import time
# https://pypi.org/project/evdev/
import evdev
string = 'We are typing this 1!'
with evdev.UInput() as ui:
escape = False
shift = False
for letter in string:
if letter == ' ':
key = evdev.ecodes.KEY_SPACE
elif letter == '\\':
escape = True
continue
elif escape and letter == 'r':
escape = False
key = evdev.ecodes.KEY_ENTER
elif letter == '!':
# Symbols are the biggest problem here since we need to know which key
# to press and if we need shift. This will change with different keyboard layouts.
key = evdev.ecodes.ecodes['KEY_1']
shift = True
else:
key = evdev.ecodes.ecodes['KEY_'+letter.upper()]
if letter.isupper() or shift:
ui.write(evdev.ecodes.EV_KEY, evdev.ecodes.KEY_LEFTSHIFT, 1)
ui.write(evdev.ecodes.EV_KEY, key, 1)
ui.write(evdev.ecodes.EV_KEY, key, 0)
time.sleep(.05)
ui.syn()
if letter.isupper() or shift:
ui.write(evdev.ecodes.EV_KEY, evdev.ecodes.KEY_LEFTSHIFT, 0)
ui.syn()
@HarlemSquirrel
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment