This repository was archived by the owner on Feb 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClickFix-old.py
More file actions
153 lines (124 loc) · 4.22 KB
/
ClickFix-old.py
File metadata and controls
153 lines (124 loc) · 4.22 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
from typing import Tuple
import psutil
import os
import math
import pythoncom
import pyWinhook as pyHook
import ctypes
# MessageName : mouse move
# Message : 512
# MessageName : mouse left down
# Message : 513
# MessageName : mouse left up
# Message : 514
MIN_DELAY: int = 150 # ms
MIN_DISTANCE: int = 9 # px
lastClickRealDownPos: Tuple[int, int] = (0, 0) # x, y
mouseState: str = 'up' # 'up', 'down'
mouseStateInSelection: str = 'down' # 'down', 'up'
mouseIsSelecting: bool = False
lastClickUpTime: int = 0
lastClickUpSelectPos: Tuple[int, int] = (0, 0)
lastClickUpSelectTime: int = 0
lastClickDownBug: bool = False # down-up bug outside selection
def messages(event):
print('MessageName:', event.MessageName)
print('Message:', event.Message)
print('Time:', event.Time)
print('Window:', event.Window)
print('WindowName:', event.WindowName)
print('Position:', event.Position)
print('Wheel:', event.Wheel)
print('Injected:', event.Injected)
print('---')
def sethighprio():
# set high priority
p = psutil.Process(os.getpid())
# print('> prio before', int(p.nice()))
p.nice(psutil.HIGH_PRIORITY_CLASS)
# print('> prio after', int(p.nice()))
def get_distance(pos_1, pos_2):
return math.sqrt((pos_2[0] - pos_1[0])**2 + (pos_2[1] - pos_1[1])**2)
def mouse_up(hndl, pos):
# ctypes.windll.user32.SetCursorPos(x, y)
hndl.UnhookMouse()
import win32con
# pos = ctypes.windll.user32.GetCursorPos()
ctypes.windll.user32.mouse_event(win32con.MOUSEEVENTF_ABSOLUTE | win32con.MOUSEEVENTF_LEFTUP, pos[0], pos[1], 0, 0)
# print('mouse_up')
hndl.HookMouse()
def onmouseevent(event):
global hm
global MIN_DELAY
global MIN_DISTANCE
global lastClickRealDownPos
global mouseState
global mouseStateInSelection
global mouseIsSelecting
global lastClickUpTime
global lastClickUpSelectPos
global lastClickUpSelectTime
global lastClickDownBug
# called when mouse events are received
# messages(event)
# print(event.Time, mouseState, mouseIsSelecting, mouseStateInSelection,
# get_distance(lastClickDownPos, event.Position), event.Message)
if get_distance(lastClickRealDownPos, event.Position) > MIN_DISTANCE and mouseState == 'down':
mouseIsSelecting = True
else:
mouseIsSelecting = False
if not mouseIsSelecting:
# mouse down
if event.Message == 513:
if event.Time - lastClickUpTime > MIN_DELAY:
lastClickRealDownPos = event.Position
mouseState = 'down'
# set high priority when clicking
sethighprio()
return True
else:
# print('bug down')
lastClickDownBug = True
return False
# mouse up
if event.Message == 514:
if lastClickDownBug:
# consume the down-up bug
# print('bug up')
lastClickDownBug = False
return False
else:
lastClickUpTime = event.Time
mouseState = 'up'
return True
else: # mouse is selecting
# mouse down
if event.Message == 513:
mouseStateInSelection = 'down'
return False
# mouse up
if event.Message == 514:
mouseStateInSelection = 'up'
lastClickUpSelectPos = event.Position
lastClickUpSelectTime = event.Time
return False
# free selecting
if mouseStateInSelection == 'up' and event.Time - lastClickUpSelectTime > MIN_DELAY * 1.3:
# print(event.Time - lastClickUpSelectTime)
mouseIsSelecting = False
mouseStateInSelection = 'down' # reset the default value
lastClickUpTime = lastClickUpSelectTime
mouseState = 'up'
mouse_up(hm, lastClickUpSelectPos)
return True
return True
# set high priority
sethighprio()
# create a hook manager
hm = pyHook.HookManager()
# watch for all mouse events
hm.MouseAll = onmouseevent
# set the hook
hm.HookMouse()
# wait forever
pythoncom.PumpMessages()