-
Notifications
You must be signed in to change notification settings - Fork 2
/
pykey.py
executable file
·139 lines (125 loc) · 3.92 KB
/
pykey.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/python
# -*- coding:utf-8; -*-
# Trimmed down version of pykey, extended to pick target window first.
#
# pykey -- a Python version of crikey,
# http://shallowsky.com/software/crikey
# Simulate keypresses under X11.
#
# This software is copyright 2008 by Akkana Peck,
# and copyright 2010 by Zbigniew Jędrzejewski-Szmek.
# Please share and re-use this under the terms of the GPLv2
# or, at your option, any later GPL version.
import sys, time
import re
import subprocess
import Xlib.display
import Xlib.X
import Xlib.XK
import Xlib.protocol.event
display = Xlib.display.Display()
def get_window_id():
xwininfo = subprocess.Popen(['xwininfo'],
stdout=subprocess.PIPE, stderr=sys.stderr)
output, errors = xwininfo.communicate()
match = re.search(r'Window id: (0x[0-9a-f]+)', output)
if match is None:
raise ValueError('cannot find window id in xwininfo output')
windowid = int(match.group(1), 16)
return windowid
def get_window(windowid=None):
#window = display.get_input_focus()._data["focus"];
if windowid is None:
windowid = get_window_id()
window = display.create_resource_object('window', windowid)
return window
special_X_keysyms = {
' ' : "space",
'\t' : "Tab",
'\n' : "Return", # for some reason this needs to be cr, not lf
'\r' : "Return",
'\e' : "Escape",
'!' : "exclam",
'#' : "numbersign",
'%' : "percent",
'$' : "dollar",
'&' : "ampersand",
'"' : "quotedbl",
'\'' : "apostrophe",
'(' : "parenleft",
')' : "parenright",
'*' : "asterisk",
'=' : "equal",
'+' : "plus",
',' : "comma",
'-' : "minus",
'.' : "period",
'/' : "slash",
':' : "colon",
';' : "semicolon",
'<' : "less",
'>' : "greater",
'?' : "question",
'@' : "at",
'[' : "bracketleft",
']' : "bracketright",
'\\' : "backslash",
'^' : "asciicircum",
'_' : "underscore",
'`' : "grave",
'{' : "braceleft",
'|' : "bar",
'}' : "braceright",
'~' : "asciitilde"
}
def get_keysym(ch):
keysym = Xlib.XK.string_to_keysym(ch)
if keysym == 0:
# Unfortunately, although this works to get the correct keysym
# i.e. keysym for '#' is returned as "numbersign"
# the subsequent display.keysym_to_keycode("numbersign") is 0.
if ch in special_X_keysyms:
special = special_X_keysyms[ch]
keysym = Xlib.XK.string_to_keysym(special)
return keysym
def is_shifted(ch):
if ch.isupper():
return True
if ch in '~!@#$%^&*()_+{}|:\"<>?':
return True
return False
def char_to_keycode(ch):
keysym = get_keysym(ch)
keycode = display.keysym_to_keycode(keysym)
if keycode == 0:
print "Sorry, can't map", ch
if is_shifted(ch):
shift_mask = Xlib.X.ShiftMask
else:
shift_mask = 0
return keycode, shift_mask
def send_string(window, str):
for ch in str:
keycode, shift_mask = char_to_keycode(ch)
if keycode == 0:
keycode, shift_mask = char_to_keycode('_')
print 'sending [{0!r}] keycode={1} with shift_mask={2}'.format(
ch, keycode, shift_mask)
for eventtype in (Xlib.protocol.event.KeyPress,
Xlib.protocol.event.KeyRelease):
event = eventtype(root=display.screen().root,
window=window,
same_screen=0,
child=Xlib.X.NONE,
root_x=0, root_y=0,
event_x=0, event_y=0,
state=shift_mask,
detail=keycode,
time=int(time.time()))
window.send_event(event, propagate=True)
if __name__ == '__main__':
for arg in sys.argv[1:]:
window = get_window()
time.sleep(3)
send_string(window, arg)
display.sync()