Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -------------------------------------------
- # (c) 2020 Fouad AMANSOUR fd.amansour@gmail.com
- # This code can retrieve the status (whether a key is up or down)
- # of keyboard and mouse buttons using Virtual-Key Codes in the background
- # Replace 0x04 in win32api.GetKeyState(0x04) everywhere in the file
- # with Virtual-Key Code of the button for which you want to retrieve the status
- # -------------------------------------------
- import win32api
- import time
- CurrentKeyState = win32api.GetKeyState(0x04) # "0x04" is the Virtual-Key Code of the Middle mouse button (VK_MBUTTON)
- while True:
- NewKeyState = win32api.GetKeyState(0x04)
- if NewKeyState != CurrentKeyState: # Button state changed
- """
- WHEN THE BUTTON IN QUESTION IS :
- * pressed the 1st time: the value of win32api.GetKeyState() is -128
- * Releases the 1st time: the value of win32api.GetKeyState() is 0
- * pressed the 2nd time: the value of win32api.GetKeyState() is -127
- * Releases the 2nd time: the value of win32api.GetKeyState() is 1
- """
- if NewKeyState < 0: # KeyState = -127 or -128
- print('BUTTON PRESSED ---', 'NewKeyState = ', NewKeyState )
- else: # KeyState =1 or 0
- print('BUTTON RELEASED ---', 'NewKeyState = ', NewKeyState)
- CurrentKeyState = NewKeyState
- time.sleep(0.001)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement