'Python detect single key press when app in focus only
I want to detect a single keypress event in my python app so that I can implement a simple menu of commands. Googling leads to the most popular solution of using python 'keyboard' module. So I can have simple code like this:
key = keyboard.read_key()
if key == 'h':
print("Help: 's' = Status 'q' = Quit")
elif(key == 's'):
print("A quick brown fox jumps over the lazy dog.")
elif(key == 'q'):
print("Quitting.....")
break
However, the problem with this code is that my app detects keys even while I am typing this question here and my script is running in the background, which is not what I intended to do.
'Keyboard' module boasts of being global in its capturing the keys and I can't find in its documentation how to limit this to only when my app is in focus. Any ideas if we can do it with the 'keyboard' module or any other way of getting the same effect?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
