I'm trying to create a reaction time game thing, however when I press enter after the hotkey is added I get a super long Nonetype error making reference to several different files.
import time
import datetime
import random
import keyboard
playing=True
def stop(playing):
playing=False
input("Press enter when you're ready to test your reaction time!")
print("Press enter when you see the cat!")
time.sleep(1)
keyboard.add_hotkey('enter', stop(playing))
time.sleep(random.randint(3,6))
if(playing==True):
keyboard.remove_hotkey('enter')
start = datetime.datetime.now()
input("Press Enter!!!!")
end = datetime.datetime.now()
print(end-start)
The idea of the hotkey is to stop the user from being able to just hold the enter button down. I don't know why this doesn't work.
I tried adding 'return playing' to the end of the function but that just gave me a similar error of 'bool is not callable'
stop(playing)
returns None
, so when you do:
keyboard.add_hotkey('enter', stop(playing))
it's exactly as if you did:
stop(playing)
keyboard.add_hotkey('enter', None)
The second argument to add_hotkey
is supposed to be a function (not the result of calling a function), so if you pass it None
, it raises an error.
What I think you want to do is define your stop
function so that it can be called with no arguments, and sets the global playing
variable to False:
def stop()
global playing
playing = False
and then pass the function (without calling it yet) to add_hotkey
:
keyboard.add_hotkey('enter', stop)