88from platform import system
99
1010if system () == 'Windows' :
11- from ctypes import windll , Structure , c_ulong , byref
11+ from ctypes import windll , Structure , c_ulong , c_ushort , POINTER , Union , byref , c_int , sizeof , c_long
1212
1313 LBUTTON = 1
1414 RBUTTON = 2
168168 KEY_BACKSLASH = 0xDC
169169 KEY_CLOSE_BRACKET = 0xDD
170170 KEY_APOSTROPHE = 0xDE
171+
172+ KEYEVENT_KEYUP = 0x0002
173+
174+ class KeybdInputType (Structure ):
175+ _fields_ = [('wVk' , c_ushort ),
176+ ('wScan' , c_ushort ),
177+ ('dwFlags' , c_ulong ),
178+ ('time' , c_ulong ),
179+ ('dwExtraInfo' , POINTER (c_ulong ))]
180+
181+ class HardwareInputType (Structure ):
182+ _fields_ = (('uMsg' , c_ulong ),
183+ ('wParamL' , c_ushort ),
184+ ('wParamH' , c_ushort ))
185+
186+ class MouseInputType (Structure ):
187+ _fields_ = (('dx' , c_long ),
188+ ('dy' , c_long ),
189+ ('mouseData' , c_ulong ),
190+ ('dwFlags' , c_ulong ),
191+ ('time' , c_ulong ),
192+ ('dwExtraInfo' , POINTER (c_ulong )))
193+
194+ class InputUnionType (Union ):
195+ _fields_ = [('mi' , MouseInputType ), ('ki' , KeybdInputType ), ('hi' , HardwareInputType )]
196+
197+ class InputType (Structure ):
198+ _fields_ = [('type' , c_ulong ),
199+ ('union' , InputUnionType )]
200+
201+ def pressKey (key ):
202+ data = (InputType * 1 )(InputType (1 , InputUnionType (ki = KeybdInputType (key ,key ,0 ,0 ,None ))))
203+ windll .user32 .SendInput (1 , data , c_int (sizeof (data )))
204+
205+ def releaseKey (key ):
206+ data = (InputType * 1 )(InputType (1 , InputUnionType (ki = KeybdInputType (key ,key ,KEYEVENT_KEYUP ,0 ,None ))))
207+ windll .user32 .SendInput (1 , data , c_int (sizeof (data )))
171208
172209 def getPressState (key ):
173210 v = windll .user32 .GetAsyncKeyState (int (key ))
@@ -209,6 +246,6 @@ def getScreenSize():
209246 now ,last = getPressState (ord (' ' ))
210247 if now or last :
211248 print (now , last )
212- print (getMousePosition (), getScreenSize ())
249+ # print(getMousePosition(), getScreenSize())
213250 sleep (0.01 )
214251
0 commit comments