11import ctypes
22from ctypes .wintypes import *
3+ from Engine .Logging import *
4+ from Engine .Byte import *
35
46# CTYPES ADAPTATE -------------------------------
57
@@ -70,8 +72,6 @@ class Input:
7072 ruCaps = "ЙЦУКЕНГШЩЗХЪФЫВАПРОЛДЖЭЯЧСМИТЬБЮ"
7173 ru = ruCaps .lower ()
7274
73- listning = True
74-
7575 class Types :
7676 """Типы событий"""
7777 Keyboard = 1
@@ -83,7 +83,7 @@ class Types:
8383 class Mouse :
8484 """События мыши"""
8585 #Mouse type:
86- CLICK = 0
86+ DOWN = 0
8787 MOVE = 1
8888 DOUBLECLICK = 2
8989 WHEELV = 4
@@ -153,25 +153,6 @@ class Focus:
153153 """События фокуса"""
154154 pass
155155
156- class Event :
157- """Событие"""
158- def __init__ (self , type = - 1 , mouseKey = - 1 , mouseX = - 1 , mouseY = - 1 , mouseType = - 1 , keyboardCode = - 1 , keyboardChar = - 1 , keyboardState = - 1 ):
159- """Событие"""
160-
161- self .type = type
162-
163- self .mouseType = mouseType
164- self .mouseKey = mouseKey
165- self .mouseX = mouseX
166- self .mouseY = mouseY
167-
168- self .keyboardCode = keyboardCode
169- self .keyboardChar = keyboardChar
170- self .keyboardState = keyboardState
171-
172- def __str__ (self ):
173- return f"Type: { self .type } \n mouseType: { self .mouseType } \n MouseKey: { self .mouseKey } \n MouseX: { self .mouseX } \n MouseY: { self .mouseY } \n KeyboardCode: { self .keyboardCode } \n KeyboardChar: { self .keyboardChar } \n KeyboardState: { self .keyboardState } \n "
174-
175156 def init (useHotkey = False , lineInput = False , echo = False , resizeEvents = False , mouseEvents = False , insert = False , quickEdit = False , extended = False ):
176157 """Включает получение событий\n Принимает: (bool) useHotkey - использование горячих клавиш, (bool) lineInput - описание отсутствует, (bool) echo - добавление в выходной массив, (bool) resizeEvents - принятие событий изменения размеров окна, (bool) mouseEvents - принятие событий мыши, (bool) insert - включает insert, (bool) quickEdit - выделение мышью, (bool) extended - запрет quickEdit"""
177158 Input .handle = ctypes .windll .kernel32 .GetStdHandle (- 10 )
@@ -189,43 +170,57 @@ def init(useHotkey=False, lineInput=False, echo=False, resizeEvents=False, mouse
189170 if quickEdit : out += 64
190171 if extended : out += 128
191172
173+ Input .varInit ()
174+
192175 ctypes .windll .kernel32 .SetConsoleMode (Input .handle , out )
193176
194- def tick (asyn = True ):
195- """Получение событий, обработка и их запись в массив\n Принимает: (bool) asyn - не ждать события"""
196- if not Input .listning : return
177+ def reset ():
178+ """Отчистка входного буффера"""
179+ prev = 0
180+ Input .tick ()
181+ while int (bytes (Input .events )[0 ]) != 0 :
182+ Input .tick ()
197183
198- ctypes .windll .kernel32 .ReadConsoleInputExW (Input .handle , ctypes .byref (Input .InputRecord ), 1 , ctypes .byref (Input .events ), 2 if asyn else 0 )
184+ Input .varInit ()
185+
186+ def varInit ():
187+ """Сброс / инициализация переменных"""
188+ Input .eventType = 0
199189
200- record = Input .InputRecord
190+ Input .mouseX = 0
191+ Input .mouseY = 0
192+ Input .mouseKey = 0
193+
194+ Input .mouseType = 0
195+
196+ Input .keyboardCode = 0
197+ Input .keyboardChar = 0
198+ Input .keyboardState = 0
199+ Input .prevKeyboardState = False
200+ Input .prevMouseState = False
201201
202- event = record . Event
203- eventType = record . EventType
202+ Input . event = 0
203+ Input . eventString = ""
204204
205- mouseX = event .MouseEvent .dwMousePosition .X # X
206- mouseY = event .MouseEvent .dwMousePosition .Y # Y
207- mouseKey = event .MouseEvent .dwButtonState # какая кнопка клавиатуры нажата
208- mouseType = event .MouseEvent .dwEventFlags # колесо / нажатие / движение / двойное нажатие
205+ def tick ():
206+ """Получение событий, обработка и их запись в массив\n Принимает: (bool) asyn - не ждать события"""
209207
210- keyboardCode = event .KeyEvent .wVirtualKeyCode # Код кнопки клавиатуры
211- keyboardChar = event .KeyEvent .uChar .UnicodeChar # Символ клавиши
212- keyboardState = event .KeyEvent .bKeyDown # Состояние кнопки
208+ ctypes .windll .kernel32 .ReadConsoleInputExW (Input .handle , ctypes .byref (Input .InputRecord ), 1 , ctypes .byref (Input .events ), 2 )
209+ record = Input .InputRecord
213210
214- event = Input .Event ( type = eventType , mouseType = mouseType , mouseKey = mouseKey , mouseX = mouseX , mouseY = mouseY , keyboardCode = keyboardCode , keyboardChar = keyboardChar , keyboardState = keyboardState )
215- Input .EVENTS . append ( event )
211+ Input . event = record .Event
212+ Input .eventType = record . EventType
216213
217- def clearEvents ():
218- """Очистка массива событий"""
219- Input .EVENTS = []
214+ Input .mouseX = Input .event .MouseEvent .dwMousePosition .X # X
215+ Input .mouseY = Input .event .MouseEvent .dwMousePosition .Y # Y
216+ Input .mouseKey = Input .event .MouseEvent .dwButtonState # какая кнопка клавиатуры нажата
217+
218+ Input .prevMouseState = Input .mouseType == Input .Mouse .DOWN
219+ Input .mouseType = Input .event .MouseEvent .dwEventFlags # колесо / нажатие / движение / двойное нажатие
220220
221- def getEvents ( tick = True ):
222- """Возвращает события \n Принимает: (bool) tick - авто получение событий"""
223- if tick :
224- Input .tick ()
221+ Input . prevKeyboardState = Input . keyboardState if Input . eventType == Input . Types . Keyboard else False
222+ Input . keyboardCode = Input . event . KeyEvent . wVirtualKeyCode # Код кнопки клавиатуры
223+ Input . keyboardChar = Input . event . KeyEvent . uChar . UnicodeChar # Символ клавиши
224+ Input .keyboardState = Input . event . KeyEvent . bKeyDown if Input . eventType == Input . Types . Keyboard else False # Состояние кнопки
225225
226- for event in Input .EVENTS :
227- if len (Input .EVENTS ):
228- ev = Input .EVENTS .pop ()
229- yield ev
230- else :
231- return []
226+ Input .eventString = f"{ Input .mouseX } { Input .mouseY } { Input .mouseKey } { Input .keyboardCode } { Input .keyboardChar } { Input .keyboardState } "
0 commit comments