Created
February 5, 2020 00:20
-
-
Save HarlemSquirrel/dbc306b9e550496e8d150992d6c6c256 to your computer and use it in GitHub Desktop.
Sample test sending keys on Wayland from string to emulate typing.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Adapted from https://github.com/meeuw/injectinput/blob/master/injectinput/injectinput.py