-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathxk.py
executable file
·185 lines (164 loc) · 5.51 KB
/
xk.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#!/usr/bin/env python
# pykey -- a Python version of crikey,
# http://shallowsky.com/software/crikey
# Simulate keypresses under X11.
#
# This software is copyright 2008 by Akkana Peck.
# Please share and re-use this under the terms of the GPLv2
# or, at your option, any later GPL version.
import Xlib.display
import Xlib.X
import Xlib.XK
import Xlib.protocol.event
UseXTest = True
try :
import Xlib.ext.xtest
except ImportError:
UseXTest = False
print "no XTest extension; using XSendEvent"
import sys, time
display = Xlib.display.Display()
window = display.get_input_focus()._data["focus"];
if UseXTest and not display.query_extension("XTEST") :
UseXTest = False
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.
keysym = Xlib.XK.string_to_keysym(special_X_keysyms[ch])
return keysym
def is_shifted(ch) :
if ch.isupper() :
return True
if "~!@#$%^&*()_+{}|:\"<>?".find(ch) >= 0 :
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 press_key(keysym, shift_mask = 0) :
keycode = display.keysym_to_keycode(keysym)
if (UseXTest) :
if shift_mask != 0 :
Xlib.ext.xtest.fake_input(display, Xlib.X.KeyPress, 50)
Xlib.ext.xtest.fake_input(display, Xlib.X.KeyPress, keycode)
else :
event = Xlib.protocol.event.KeyPress(
time = int(time.time()),
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
)
window.send_event(event, propagate = True)
display.sync()
def release_key(keysym, shift_mask = 0) :
keycode = display.keysym_to_keycode(keysym)
if (UseXTest) :
Xlib.ext.xtest.fake_input(display, Xlib.X.KeyRelease, keycode)
if shift_mask != 0 :
Xlib.ext.xtest.fake_input(display, Xlib.X.KeyRelease, 50)
else :
event = Xlib.protocol.event.KeyRelease(
time = int(time.time()),
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
)
window.send_event(event, propagate = True)
display.sync()
def send_string(str) :
for ch in str :
#print "sending", ch, "=", display.keysym_to_keycode(Xlib.XK.string_to_keysym(ch))
keycode, shift_mask = char_to_keycode(ch)
if (UseXTest) :
#print "Trying fake_input of", ch, ", shift_mask is", shift_mask
if shift_mask != 0 :
Xlib.ext.xtest.fake_input(display, Xlib.X.KeyPress, 50)
Xlib.ext.xtest.fake_input(display, Xlib.X.KeyPress, keycode)
Xlib.ext.xtest.fake_input(display, Xlib.X.KeyRelease, keycode)
if shift_mask != 0 :
Xlib.ext.xtest.fake_input(display, Xlib.X.KeyRelease, 50)
else :
event = Xlib.protocol.event.KeyPress(
time = int(time.time()),
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
)
window.send_event(event, propagate = True)
event = Xlib.protocol.event.KeyRelease(
time = int(time.time()),
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
)
window.send_event(event, propagate = True)
display.sync()
def press_button(button):
Xlib.ext.xtest.fake_input(display, Xlib.X.ButtonPress, button)
display.sync()
Xlib.ext.xtest.fake_input(display, Xlib.X.ButtonRelease, button)
display.sync()
if __name__ == "__main__":
for argp in range(1, len(sys.argv)) :
send_string(sys.argv[argp])