Search code examples
pythonpynput

How do i see if a key is pressed down using Pynput in Python?


I want to (by using Pynput in Python) see if a key is pressed down. For example: while True: if #key alt is pressed down: print("Alt is pressed down")


Solution

  • To install pynput in python use following command

    pip install pynput
    

    And to check if a key alt is pressed below is my code, for more details refer to the link

    from pynput.keyboard import Key, Listener
    
    def on_press(key):
        print('{0} pressed'.format(
            key))
    
    with Listener(
            on_press=on_press) as listener:
        listener.join()