-
Notifications
You must be signed in to change notification settings - Fork 0
/
kecomp.py
executable file
·251 lines (223 loc) · 7.59 KB
/
kecomp.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#!/usr/bin/env python
from pykeyboard import PyKeyboardEvent
from pymouse import PyMouse
from enum import Enum
from threading import Thread
from time import sleep
import os
import argparse
def readconfig(path):
with open(path, 'r') as f:
contents = f.read()
return contents
def defaultconfig():
"""
If there is a kecomp.conf in the user's working directory, use it.
Else, if there is a kecomp.conf in the same directory as this file, use it.
Else, use the fallback configuration defined below.
"""
fallback = """
steer <Up> 0 -1
steer <Down> 0 1
steer <Left> -1 0
steer <Right> 1 0
default speed 15
speed B 30
speed C /3
speed V *0.2
map Z <LeftMouse>
map X <RightMouse>
refreshrate 10
"""
filename = "kecomp.conf"
path = os.path.realpath(os.path.join(os.getcwd(), filename))
if not os.path.exists(path):
path = os.path.realpath(os.path.join(os.getcwd(),
os.path.dirname(__file__),
filename))
if not os.path.exists(path):
return fallback
contents = readconfig(path)
return contents
def adaptsym(sym):
""" Translate Vim sym notation into PyUserInput notation. """
syms = {
"<Up>": "Up",
"<Right>": "Right",
"<Down>": "Down",
"<Left>": "Left",
}
if sym in syms:
return syms[sym]
def lettermap(c):
if os.name == 'posix':
return c.lower()
elif os.name == 'nt':
return c.upper()
else:
raise("Unknown operating system.")
return lettermap(sym)
def modifier(string):
""" Parse a string into either a value or an operation. """
try:
speed = float(string)
return speed
except ValueError:
operation = string[0]
multiplier = float(string[1:])
if operation == '*':
def operation(speed):
return speed*multiplier
elif operation == '/':
def operation(speed):
return speed/multiplier
else:
raise("Unknown modifier string format: %s" % string)
return operation
def parseconfig(config):
conf = {'speed': dict(), 'steer': dict(), 'map': dict()}
for line in config.strip().splitlines():
tokens = line.split()
if tokens[0] == 'steer':
(key, x, y) = tokens[1:]
conf['steer'][adaptsym(key)] = (int(x), int(y))
elif tokens[0] == 'speed':
(key, speedstr) = tokens[1:]
conf['speed'][adaptsym(key)] = modifier(speedstr)
elif tokens[0] == 'map':
(fromkey, tokey) = tokens[1:]
conf['map'][adaptsym(fromkey)] = tokey
elif tokens[0] == 'refreshrate':
ratemilliseconds = tokens[1]
conf['refreshrate'] = float(ratemilliseconds)/1000
elif tokens[0] == 'default' and tokens[1] == 'speed':
defaultspeed = tokens[2]
conf['defaultspeed'] = int(defaultspeed)
return conf
Action = Enum('Action', 'press release init')
class KeyListener(PyKeyboardEvent):
def __init__(self):
PyKeyboardEvent.__init__(self)
self.callno = 0
self.pressedkeys = set()
def handler(self, reply):
self.callno = self.callno + 1
if os.name == 'posix':
self.x11handler(reply)
elif os.name == 'nt':
self.windowshandler(reply)
return True
else:
raise("Unknown operating system.")
def windowshandler(self, reply):
if reply.IsTransition():
action = Action.release
else:
action = Action.press
key = reply.GetKey()
continuous = action == Action.press and key in self.pressedkeys
if continuous:
return # We are not concerned with these kinds of events
self.pressedkeys.add(key)
if key not in self.pressedkeys and action == Action.press:
self.pressedkeys.add(key)
elif key in self.pressedkeys and action == Action.release:
self.pressedkeys.remove(key)
def x11handler(self, reply):
r = reply._data['data']
if not r:
return
continuouscode = r[2]
if continuouscode == 1:
continuous = True
elif continuouscode == 0:
continuous = False
else:
raise("Unknown continuous code: %d" % continuouscode)
if continuous:
return # We are not concerned with these kinds of events
actioncode = r[0]
if actioncode == 2:
action = Action.press
elif actioncode == 3:
action = Action.release
else:
raise("Unknown action code: %d" % actioncode)
keycode = r[1]
key = self.lookup_char_from_keycode(keycode)
if key not in self.pressedkeys and action == Action.press:
self.pressedkeys.add(key)
elif key in self.pressedkeys and action == Action.release:
self.pressedkeys.remove(key)
class Mouse(PyMouse):
def __init__(self):
PyMouse.__init__(self)
self.leftmousepressed = False
self.rightmousepressed = False
def press(self, x, y, button=1):
PyMouse.press(self, x, y, button)
if button == 1:
self.leftmousepressed = True
elif button == 2:
self.rightmousepressed = True
def release(self, x, y, button=1):
PyMouse.release(self, x, y, button)
if button == 1:
self.leftmousepressed = False
elif button == 2:
self.rightmousepressed = False
def update(conf, mouse, keylistener):
"""
Updates the mouse pointer according to the set of currently pressed keys.
"""
(x, y) = mouse.position()
(dx, dy) = (0, 0)
speed = conf['defaultspeed'] if 'defaultspeed' in conf else 1
leftpress = False
rightpress = False
for pressedkey in keylistener.pressedkeys:
if pressedkey in conf['steer']:
(ddx, ddy) = conf['steer'][pressedkey]
(dx, dy) = (dx+ddx, dy+ddy)
if pressedkey in conf['speed']:
try:
speed = float(conf['speed'][pressedkey])
except TypeError:
f = conf['speed'][pressedkey]
speed = f(speed)
if pressedkey in conf['map']:
if conf['map'][pressedkey] == '<LeftMouse>':
leftpress = True
if conf['map'][pressedkey] == '<RightMouse>':
rightpress = True
newx = round(x+speed*dx)
newy = round(y+speed*dy)
mouse.move(newx, newy)
if leftpress and not mouse.leftmousepressed:
mouse.press(newx, newy, 1)
elif not leftpress and mouse.leftmousepressed:
mouse.release(newx, newy, 1)
if rightpress and not mouse.rightmousepressed:
mouse.press(newx, newy, 2)
elif not rightpress and mouse.rightmousepressed:
mouse.release(newx, newy, 2)
def run(config=defaultconfig()):
""" Run the program with the specified configuration. """
conf = parseconfig(config)
keylistener = KeyListener()
t = Thread(target=keylistener.run)
t.start()
mouse = Mouse()
while True:
update(conf, mouse, keylistener)
sleep(conf['refreshrate'])
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description="KEyboard COntrolled Mouse Pointer.")
parser.add_argument('-c', '--config', help="Path to configuration file.")
args = parser.parse_args()
if args.config:
config = readconfig(args.config)
run(config)
else:
run()