-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmouse.py
More file actions
91 lines (75 loc) · 2.59 KB
/
Copy pathmouse.py
File metadata and controls
91 lines (75 loc) · 2.59 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
import ctypes
import ctypes.wintypes
import time
from ctypes import windll
import win32api
import win32con
# Load Windows API functions
user32 = windll.user32
kernel32 = windll.kernel32
def is_acs_in_focus():
"""Check if the foreground window belongs to 'acs.exe'."""
hwnd = user32.GetForegroundWindow()
if hwnd == 0:
return False
pid = ctypes.wintypes.DWORD()
user32.GetWindowThreadProcessId(hwnd, ctypes.byref(pid))
process_id = pid.value
PROCESS_QUERY_INFORMATION = 0x0400
PROCESS_VM_READ = 0x0010
h_process = kernel32.OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, False, process_id)
if not h_process:
return False
exe_name = ctypes.create_string_buffer(512)
size = ctypes.sizeof(exe_name)
psapi = windll.psapi
if psapi.GetModuleFileNameExA(h_process, None, exe_name, size):
kernel32.CloseHandle(h_process)
exe_path = exe_name.value.decode('utf-8')
return exe_path.lower().endswith("acs.exe")
kernel32.CloseHandle(h_process)
return False
def confine_cursor_to_window():
"""Locks the cursor inside the 'acs.exe' window."""
hwnd = user32.GetForegroundWindow()
rect = ctypes.wintypes.RECT()
if user32.GetWindowRect(hwnd, ctypes.byref(rect)):
user32.ClipCursor(ctypes.byref(rect)) # Restrict cursor movement
def release_cursor():
"""Releases the cursor, allowing normal movement."""
user32.ClipCursor(None)
def hide_cursor():
"""Hides the mouse cursor."""
user32.ShowCursor(False)
def show_cursor():
"""Shows the mouse cursor."""
user32.ShowCursor(True)
def block_clicks():
"""Blocks mouse clicks outside Assetto Corsa."""
while is_acs_in_focus():
win32api.BlockInput(True) # Disable mouse input outside the game
time.sleep(0.01)
win32api.BlockInput(False) # Restore mouse input when the game loses focus
def main():
"""Main loop to check if Assetto Corsa is in focus and manage mouse behavior."""
mouse_locked = False
while True:
if is_acs_in_focus():
if not mouse_locked:
confine_cursor_to_window()
hide_cursor()
mouse_locked = True
else:
if mouse_locked:
release_cursor()
show_cursor()
mouse_locked = False
time.sleep(0.1) # Small delay to prevent high CPU usage
if __name__ == "__main__":
print("Mouse confinement active. Press CTRL+C to exit.")
try:
main()
except KeyboardInterrupt:
release_cursor()
show_cursor()
print("\nExiting...")