-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmouse_yoke.py
More file actions
170 lines (128 loc) · 4.84 KB
/
mouse_yoke.py
File metadata and controls
170 lines (128 loc) · 4.84 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
from pynput import mouse, keyboard
from threading import Thread
import pydirectinput
import vgamepad as vg
import sys
import time
import ctypes
configs = {
"center_xy_axes_key": keyboard.KeyCode.from_char('/'),
"rudder_mode": keyboard.KeyCode.from_char('\\') # keyboard.Key.ctrl_l
}
gamepad = vg.VX360Gamepad()
(screen_size_width, screen_size_height) = pydirectinput.size()
global_x = 0
global_y = 0
active = False
mainmode = True
mainmode_rudder = False
pydirectinput.FAILSAFE = False
pixelsToFloatX = 0.0
pixelsToFloatY = 0.0
last_x_position = screen_size_width / 2
last_y_position = screen_size_height / 2
last_x_position_mode = screen_size_width / 2
user32 = ctypes.WinDLL('User32.dll')
def is_capslock_on():
return True if user32.GetKeyState(0x14) else False
def turn_capslock_off():
if is_capslock_on():
user32.keybd_event(0x14, 0X3a, 0X1, 0)
user32.keybd_event(0x14, 0X3a, 0X3, 0)
def is_scrolllock_on():
return True if user32.GetKeyState(0x91) else False
def turn_scrolllock_off():
if is_capslock_on():
user32.keybd_event(0x91, 0X46, 0X1, 0)
user32.keybd_event(0x91, 0X46, 0X3, 0)
def is_numlock_on():
return True if user32.GetKeyState(0x90) else False
def turn_numlock_off():
if is_numlock_on():
user32.keybd_event(0x90, 0X45, 0X1, 0)
user32.keybd_event(0x90, 0X45, 0X3, 0)
def mouseYoke(x, y):
global pixelsToFloatX, pixelsToFloatY
global global_x, global_y
global_x = x
global_y = y
if active:
if x >= 0 and x <= screen_size_width:
pixelsToFloatX = x / (screen_size_width / 2) - 1
if y >= 0 and y <= screen_size_height:
pixelsToFloatY = y / (screen_size_height / 2) - 1
if mainmode:
gamepad.left_joystick_float(x_value_float=pixelsToFloatX, y_value_float=pixelsToFloatY)
if mainmode_rudder:
gamepad.right_joystick_float(x_value_float=0, y_value_float=pixelsToFloatX)
else:
gamepad.right_joystick_float(x_value_float=0, y_value_float=pixelsToFloatX)
if last_x_position_mode >= 0 and last_x_position_mode <= screen_size_width:
gamepad.left_joystick_float(x_value_float=last_x_position_mode / (screen_size_width / 2) - 1, y_value_float=pixelsToFloatY)
gamepad.update()
def onKeyPress(key):
global mainmode
global last_x_position_mode
if active and mainmode and key == configs["rudder_mode"]:
mainmode = False
x = last_x_position_mode
last_x_position_mode = global_x
pydirectinput.moveTo(int(x), int(global_y))
def onKeyRelease(key):
global active
global mainmode, mainmode_rudder
global last_x_position_mode
global last_x_position, last_y_position
if key == keyboard.Key.scroll_lock:
active = not active
if not active:
last_x_position = global_x
last_y_position = global_y
else:
last_x_position_mode = last_x_position
pydirectinput.moveTo(int(last_x_position), int(last_y_position))
elif key == keyboard.Key.caps_lock:
mainmode_rudder = not mainmode_rudder
elif key == configs["center_xy_axes_key"] and active:
if mainmode:
pydirectinput.moveTo(int(screen_size_width / 2), int(screen_size_height / 2))
else:
pydirectinput.moveTo(int(screen_size_width / 2), int(global_y))
elif key == configs["rudder_mode"] and active and not mainmode:
mainmode = True
x = last_x_position_mode
last_x_position_mode = global_x
pydirectinput.moveTo(int(x), int(global_y))
def userInterface():
global active
global mainmode_rudder
while (True):
if is_capslock_on():
mainmode_rudder = True
else:
mainmode_rudder = False
if is_scrolllock_on():
active = True
else:
active = False
print("active: ", active)
print("mainmode: ", mainmode)
print("mainmode_rudder: ", mainmode_rudder)
print(f"+{'':—^60}+")
print(f"|{'Axis':^20}{'Raw input':^20}{'Conversion':^20}|")
print(f"+{'':—^60}+")
print(f"|{'X':^20}{global_x:^20}{'{:.2f}'.format((pixelsToFloatX + 1) * 50) + '%':^20}|")
print(f"|{'Y':^20}{global_y:^20}{'{:.2f}'.format((pixelsToFloatY + 1) * 50) + '%':^20}|")
print(f"+{'':—^60}+")
time.sleep(10)
if __name__ == "__main__":
turn_scrolllock_off()
turn_capslock_off()
ui = Thread(target=userInterface)
ms = mouse.Listener(on_move=mouseYoke)
kb = keyboard.Listener(on_release=onKeyRelease, on_press=onKeyPress)
ms.start()
kb.start()
ui.start()
ms.join()
kb.join()