0
keypress = False

while True:
    if keypress and not keyboard.is_pressed('print screen'):
        keypress = False
    elif keyboard.is_pressed(key) and not keypress:
        keypress = True
        print(keyboard.read_key())

I want to use this code in a function something like below

keypress = False
def detect_key():
        if keypress and not keyboard.is_pressed('print screen'):
            keypress = False
        elif keyboard.is_pressed(key) and not keypress:
            keypress = True
            return keyboard.read_key()

In my case I will be calling "detect_key()" function in another file where this function will be in a while loop so whenever the 'print screen' button is pressed this function should return the key string I want to print the key only once I press and release they but on long press. So now the issue is I want to declare the '

keypress

' as a global variable but I'm not able to because even if I declare it will be overrided any suggestions please ?

  • 1
    Does this answer your question? [How to detect key release with python ( not keypress)?](https://stackoverflow.com/questions/45584937/how-to-detect-key-release-with-python-not-keypress) – ljmc Aug 11 '22 at 19:06
  • i can use listener but I'm not sure how to get a return value from the listener. I'm only able to print the value if the event is true – karthi_chellaiah Aug 12 '22 at 07:42

0 Answers0