4 # based on pykey, see also "crikey" and http://shallowsky.com/blog/tags/crikey/
9 import Xlib.protocol.event
17 print "no XTest extension; using XSendEvent"
25 '\n' : "Return", # for some reason this needs to be cr, not lf
63 keysym = Xlib.XK.string_to_keysym(ch)
65 # Unfortunately, although this works to get the correct keysym
66 # i.e. keysym for '#' is returned as "numbersign"
67 # the subsequent display.keysym_to_keycode("numbersign") is 0.
68 keysym = Xlib.XK.string_to_keysym(special_X_keysyms[ch])
74 if "~!@#$%^&*()_+{}|:\"<>?".find(ch) >= 0 :
79 def __init__(self, display = None):
82 self.display = display
84 self.display = Xlib.display.Display()
86 self.UseXTest = UseXTest
88 if UseXTest and not self.display.query_extension("XTEST") :
95 self.window = self.display.get_input_focus()._data["focus"];
97 def char_to_keycode(self, ch):
98 keysym = get_keysym(ch)
99 keycode = self.display.keysym_to_keycode(keysym)
101 print "fakeinput: Sorry, can't map", ch
103 if (is_shifted(ch)) :
104 shift_mask = Xlib.X.ShiftMask
108 return keycode, shift_mask
110 def send_char(self, ch, dosync = True) :
111 keycode, shift_mask = self.char_to_keycode(ch)
114 Xlib.ext.xtest.fake_input(self.display, Xlib.X.KeyPress, 50)
115 Xlib.ext.xtest.fake_input(self.display, Xlib.X.KeyPress, keycode)
116 Xlib.ext.xtest.fake_input(self.display, Xlib.X.KeyRelease, keycode)
118 Xlib.ext.xtest.fake_input(self.display, Xlib.X.KeyRelease, 50)
120 event = Xlib.protocol.event.KeyPress(
121 time = int(time.time()),
122 root = self.display.screen().root,
123 window = self.window,
124 same_screen = 0, child = Xlib.X.NONE,
125 root_x = 0, root_y = 0, event_x = 0, event_y = 0,
129 self.window.send_event(event, propagate = True)
130 event = Xlib.protocol.event.KeyRelease(
131 time = int(time.time()),
132 root = self.display.screen().root,
133 window = self.window,
134 same_screen = 0, child = Xlib.X.NONE,
135 root_x = 0, root_y = 0, event_x = 0, event_y = 0,
139 self.window.send_event(event, propagate = True)
143 def send_str(self, str):
145 self.send_char(ch, dosync = False)