Search code examples
pythonkeyboard

I'm using the keyboard module for python, how can I refer to arrow keys?


I need to press and release the right arrow key for a little project, but I don't know what text to use for arrow keys.

I've tried to say stuff like "right arrow", "K_RIGHT", and other things but it just gives me some huge error that I can't read through. I am using Visual Studio Code, incase anyone needs to know that.

Here's code if needed:

import keyboard
def moveArrow(times):
    while times > 0:
        keyboard.press_and_release("right")
        times = times - 1
keyboard.add_hotkey("ctrl+shift+;", moveArrow, 20, True)

keyboard.wait("esc")

Solution

  • You can use 'left', 'up', 'down', and 'right' as the text for the arrow keys.

    Note that there may be some issues between the arrow keys and the numpad keys, as described here: https://github.com/boppreh/keyboard/issues/450

    In certain applications which ignore numlock, pressing '6' on the numpad (for example) will always register a '6' regardless of whether you have numlock on or not. These same applications will respect the arrow keys if explicitly pressed (i.e., via the arrows and not the number pad). However, using keyboard.press_and_release('right') will cause a numpad6 press in these applications, as opposed to the expected arrow key.

    Please add the error you are receiving to your answer, if these key names are not working for you.