-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmbkeys.py
More file actions
283 lines (219 loc) · 7.25 KB
/
mbkeys.py
File metadata and controls
283 lines (219 loc) · 7.25 KB
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import os
import sys
import time
import winsound
import win32api
import win32con
import win32gui
# import keyboard
from pynput.keyboard import Key, Listener
# from keyboard import is_pressed
from mbk_config import MBKConfig
version = "0.1.2" # Program version
author = 'By Massimo Mula, 2021-24'
soundstatus = {True: 'ON', False: 'OFF'}
keys_down = []
key_down = None
# activation_key = 145 # 145 = Scroll Lock
# TODO - Implement QUIT key
emu_enabled = False # Boolean, used to tell whether the mouse emulation is active (True) or not (False).
setup_mode = False # Boolean. True: program is in "key mapping" mode" (listening for keys to be assigned to mouse buttons)
# Read configuration from INI file
programsettings = MBKConfig() # TODO - Implement configuration menu
# Apply Key configuration
setup_key = [['Key.ctrl_l', "'\\x13'"], ['Key.ctrl_r', "'\\x13'"]]
reset_key = ['<88>', 'Key.ctrl_l', 'Key.shift', 'Key.alt_l']
sound_key = ['<83>', 'Key.ctrl_l', 'Key.shift', 'Key.alt_l']
# activation_key = f"Key.{programsettings.get_option('Settings','ActivationKey')}"
activation_key = int(programsettings.get_option('Settings', 'ActivationKey')) # Key used to toggle emulation.
sound_enable = bool(programsettings.get_option('Settings', 'ClickSound')) # True: Enable sound; False: disable.
click_freq = programsettings.get_option('Settings', 'ClickSoundFrequency')
click_sound = [int(click_freq), 2]
mouse_mapping = {
'left': int(programsettings.get_option('MouseButtons', 'Left')),
'right': int(programsettings.get_option('MouseButtons', 'Right'))
}
# --------------------------------------------------------------------------------------------------
def clear():
lambda: os.system('cls') # on Windows System
# os.system('clear') #on Linux System
def print_configuration():
welcome_msg = f"MouseButton Keys v.{version} -- {author}"
print('\n')
print(welcome_msg)
print('\n')
print(f" Left Mouse Button is mapped to '{mouse_mapping['left']}' key")
print(f" Right Mouse Button is mapped to '{mouse_mapping['right']}' key")
print('\n')
print(f'\rSound is {soundstatus[sound_enable]} ')
print("\n Press CTRL+S to change configuration.")
print('\n')
def key_vk(key):
# Returns the Virtual Key (VK) code of the pressed key
if isinstance(key, Key):
vk = key.value.vk
else:
vk = key.vk
return vk
def toggle_emulation():
"""
Toggles emu_enabled flag when activation key is pressed (default: Scroll Lock)
:return:
"""
global emu_enabled
emu_status = ''
emu_enabled = not emu_enabled
clear()
if emu_enabled:
emu_status = '\rEmulation is ACTIVE. \n'
if sound_enable:
winsound.Beep(3000, 20)
else:
emu_status = '\rEmulation is inactive. \n'
if sound_enable:
winsound.Beep(800, 20)
print(emu_status, end='')
time.sleep(.005)
def toggle_setupmode():
"""
Toggles setup_mode flag when Setup key is pressed (CTRL+S)
:return:
"""
global setup_mode
setup_mode = not setup_mode
setup_status = ''
if setup_mode:
clear()
setup_status = '\nEntering Setup Mode.\n'
if sound_enable:
winsound.Beep(3000, 20)
else:
sys.stdout.write('\nExiting Setup Mode.\n')
if sound_enable:
winsound.Beep(500, 20)
print(setup_status)
def toggle_sound():
"""
Turns key sound on/off
:return:
"""
global sound_enable
sound_enable = not sound_enable
sys.stdout.write(f'\rSound is {soundstatus[sound_enable]} ')
if sound_enable:
winsound.Beep(3000, 20)
time.sleep(0.5)
def check_reset():
global keys_down
reset = True
for k in reset_key:
if k not in keys_down:
reset = False
break
if reset:
sys.stdout.write('\rResetting Keys_Down')
keys_down = []
sys.stdout.write(f'\r{keys_down} ')
if sound_enable:
winsound.Beep(3000, 10)
time.sleep(0.5)
def check_sound():
toggle = True
for k in sound_key:
if k not in keys_down:
toggle = False
break
if toggle:
toggle_sound()
def on_press(key):
"""
Detect pressed key
:param key: Key pressed (KeyCode)
:return:
"""
global key_down
global keys_down
global listener
listener._suppress = False
kp = key_vk(key)
if kp == activation_key:
toggle_emulation()
if emu_enabled:
if kp not in keys_down:
keys_down.append(kp)
sys.stdout.write(f'\r{keys_down} ')
check_reset()
check_sound()
if kp == mouse_mapping['left']:
# Left mouse button down
click(win32con.MOUSEEVENTF_LEFTDOWN)
elif kp == mouse_mapping['right']:
# Right mouse button down
click(win32con.MOUSEEVENTF_RIGHTDOWN)
else:
listener._suppress = False
else:
hw = window_under_cursor()
wt = title(hw)
got_focus = my_title == wt
if kp not in keys_down:
keys_down.append(kp)
if emu_enabled:
sys.stdout.write(f'\r{keys_down} ')
if got_focus and keys_down in setup_key:
toggle_setupmode()
# time.sleep(0.002)
def on_release(key):
"""
Detect released key
:param key: Key released (KeyCode)
:return:
"""
global key_down
global keys_down
global listener
kr = key_vk(key)
if emu_enabled:
if kr == mouse_mapping['left']:
# Left mouse button up
click(win32con.MOUSEEVENTF_LEFTUP)
elif kr == mouse_mapping['right']:
# Left mouse button up
click(win32con.MOUSEEVENTF_RIGHTUP)
if kr in keys_down:
keys_down.remove(kr)
if emu_enabled:
sys.stdout.write(f'\r{keys_down} ')
# time.sleep(0.002)
def click(mouseevent):
curpos = win32api.GetCursorPos()
win32api.mouse_event(mouseevent, curpos[0], curpos[1], 0, 0)
time.sleep(.005)
if sound_enable:
winsound.Beep(click_sound[0], click_sound[1])
def window_under_cursor():
"""
Grabs the window under the cursor. Returns the window on success; Returns None on error
"""
try:
hw = win32gui.WindowFromPoint(win32api.GetCursorPos())
return hw
except win32gui.error:
pass
except win32api.error:
pass
def title(hwnd):
return win32gui.GetWindowText(hwnd)
# -----------------------------------------------------------------------------------------------------
#
my_hwnd = win32gui.GetForegroundWindow()
my_title = win32gui.GetWindowText(my_hwnd)
print_configuration()
# Get Activaton key status
if win32api.GetKeyState(activation_key):
toggle_emulation()
if not emu_enabled:
print('Emulation is inactive.\n')
# Listen to events until released
with Listener(on_press=on_press, on_release=on_release) as listener: #, win32_event_filter=win32_event_filter) as listener:
listener.join()