import ctypes
def mouse_released() -> bool:
mouse_state = ctypes.windll.user32.GetAsyncKeyState(0x01)
if mouse_state:
while ctypes.windll.user32.GetAsyncKeyState(0x01):
continue # is pressing
else:
print('released')
return mouse_state != 0 # released
return False
while True:
mouse_released()
This program tells if the left mouse button was released.
Problem: in general it works correctly. But sometimes at the very beginning it prints “released”, even though nothing was pressed even a few seconds before.
I tried to change the GetAsyncKeyState
with GetKeyState
. It seemed to solve the mentioned problem, but this way, the function indicates the button being pressed only every second time, which is a complete mistery to me.
Владислав is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.