Reputation: 85
While I'm using the code below, python takes 30% of the processor speed. Is there a way to avoid that?
import keyboard
while True:
try:
if keyboard.is_pressed('ctrl+shift+m'):
print('You Pressed ctrl+shift+m')
break
except:
break
Upvotes: 0
Views: 58
Reputation: 117
Using keyboard.wait and while loop works well:
import keyboard
while True:
keyboard.wait('ctrl+shift+m')
print('You Pressed ctrl+shift+m')
Upvotes: 0
Reputation: 48662
is_pressed
always returns True
or False
immediately. It doesn't wait for keys to be pressed or released. Thus, your code is constantly in a loop, checking as often as it can whether those keys are pressed. Instead of is_pressed
, use wait
:
import keyboard
keyboard.wait('ctrl+shift+m')
print('You Pressed ctrl+shift+m')
Upvotes: 3
Reputation: 43533
Your code is spinning in a busy loop.
Add a call to time.sleep(0.1)
at the beginning of the while loop.
That will check for keypresses approximately 10x per second, and sleep the rest of the time.
Upvotes: -1