forked from akkana/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpykey
executable file
·130 lines (115 loc) · 3.62 KB
/
pykey
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
#!/usr/bin/env python
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 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)
for argp in range(1, len(sys.argv)) :
send_string(sys.argv[argp])
display.sync()