#!/usr/bin/env python import sys import time from Xlib import X, display, XK import dbg def run(): dpy = display.Display() def c_to_string_lst( c ): dic = { ' ': 'space', '\n': 'Return', '\t': 'Tab', '\b': 'BackSpace', '_': 'underscore', } if c in dic: c = dic.get( c ) lst = [ c ] if c.isupper() or c == 'underscore': lst = [ 'Shift_L' ] + lst return lst def string_to_kcode( s ): ksym = XK.string_to_keysym( s ) kcode = dpy.keysym_to_keycode( ksym ) return kcode def fake_kcode( ev, kcode, wait_sec ): dpy.xtest_fake_input( ev, detail=kcode ) dpy.flush() time.sleep( wait_sec ) def press_release_kcodes( kcodes, wait_sec ): for kcode in kcodes: fake_kcode( X.KeyPress, kcode, wait_sec ) for kcode in kcodes[ :: -1 ]: fake_kcode( X.KeyRelease, kcode, wait_sec ) wait_sec = 0.01 s = sys.stdin.read() for c in s: lst = c_to_string_lst( c ) kcodes = list( map( string_to_kcode, lst ) ) press_release_kcodes( kcodes, wait_sec ) if __name__ == "__main__": run() # EOF