forked from Kitiketov/Nand2Paint
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPyEmulator.py
More file actions
102 lines (85 loc) · 3.1 KB
/
Copy pathPyEmulator.py
File metadata and controls
102 lines (85 loc) · 3.1 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
import math
import time
from math import *
import pyautogui
from pyautogui import Point
from pynput import mouse, keyboard
from pynput.keyboard import Controller as KeyboardController
from pynput.keyboard import Key
from pynput.mouse import Controller as MouseController
class Main:
def __init__(self):
self.mouse = Mouse()
self.board = KeyboardPressed()
self.listener = keyboard.Listener(on_press=self.on_key_press)
self.listener.start()
def on_key_press(self, key):
try:
if key.char == 'q' and not self.board.can_pressed:
print('Включен')
self.board.can_pressed = True
elif key.char == 'e' and self.board.can_pressed:
print('Отключен')
self.board.can_pressed = False
except:
pass
def run(self):
while True:
if self.mouse.update_mouse():
self.board.emulate(self.mouse.direction)
class KeyboardPressed:
def __init__(self):
self.can_pressed = False
self.last_button = None
self.keyboard = KeyboardController()
self.dict = {
Point(1, 0): 'd',
Point(0, 1): 'w',
Point(-1, 0): 'a',
Point(0, -1): 's',
Point(-1, 1): 'y',
Point(1, 1): 'u',
Point(-1, -1): 'h',
Point(1, -1): 'j',
Point(0, 0): None
}
def emulate(self, direction: Point):
if not self.can_pressed:
if self.last_button is not None:
self.keyboard.release(self.last_button)
self.last_button = None
return
current_key = self.dict.get((direction.x, direction.y))
if current_key != self.last_button:
if self.last_button is not None:
self.keyboard.release(self.last_button)
if current_key is not None:
self.keyboard.press(current_key)
self.last_button = current_key
class Mouse:
def __init__(self):
self.mouse = MouseController()
self.last_position = self.mouse.position
self.direction = Point(0, 0)
self.last_time = time.time()
self.sign = lambda x: (x > 0) - (x < 0)
self.angle_threshold = math.radians(20)
def update_mouse(self) -> bool:
if time.time() - self.last_time < 0.03:
return False
now_position = pyautogui.position()
delta_x = now_position[0] - self.last_position[0]
delta_y = self.last_position[1] - now_position[1]
angle = math.atan2(abs(delta_y), abs(delta_x))
if angle < self.angle_threshold or angle > (math.pi/2 - self.angle_threshold):
if abs(delta_x) > abs(delta_y):
self.direction = Point(self.sign(delta_x), 0)
else:
self.direction = Point(0, self.sign(delta_y))
else:
self.direction = Point(self.sign(delta_x), self.sign(delta_y))
self.last_position = now_position
self.last_time = time.time()
return True
if __name__ == '__main__':
Main().run()