How to press all four of those keys:

from pynput.keyboard import Key, Controller
kb = Controller()
kb.press(Key.up) # Presses "up" key
kb.release(Key.up) # Releases "up" key
kb.press(Key.left) # Presses "left" key
kb.release(Key.left) #etc..
kb.press(Key.right)
kb.release(Key.right)
kb.press(Key.down)
kb.release(Key.down)

You can make it easier by creating a function if you need use it many times:

from pynput.keyboard import Key, Controller
kb = Controller()
def press(button):
    kb.press(button)
    kb.release(button)
# Then you can use it in one line:
press(Key.left)
# It will automatically press and release the left key.

Hope I helped.

Answer from waevarc on Stack Overflow
Discussions

GlobalHotKeys doesn't like Arrow Keys
Description GlobalHotKeys doesn't like , , , or , and possibly some other special keys. They just don't work. (Some special keys such as work fine, I don't have a comprehensive list of which keys this affects yet.) Platform and pynput version Windows 10 LTSC x64 ... More on github.com
🌐 github.com
7
January 17, 2022
May I virtualization a Down arrow key pre… - Apple Community
Mac Stuido do not respond to an USB apple Keyboard t I would need help using my apple USB keyboard with a brand-new Mac Studio, which does not recognise the keyboard that works with a Mac mini. 2801 2 ... Finally, I found a solution. I create a python script using pynput LIB, running on python3. More on discussions.apple.com
🌐 discussions.apple.com
November 21, 2021
Some keyboard presses combination doesn't work e.g shift+arrow
I'm using latest pynput(1.4) and when i try to do a key press that combined from modifier+key where key is not a letter doesn't work. The code i tried: from pynput.keyboard import Key, Cont... More on github.com
🌐 github.com
3
August 13, 2018
How to interrupt user input when the up arrow key is pressed and capture the input?
Check out this article https://stackoverflow.com/questions/70557513/creating-a-custom-keyboard-interrupt-key-in-python I think this is what you are looking for? More on reddit.com
🌐 r/learnpython
1
1
December 19, 2022
🌐
pytz
pythonhosted.org › pynput › keyboard.html
Handling the keyboard — pynput 1.1.2 documentation
An up arrow key. class pynput.keyboard.KeyCode(vk=0, char=None, is_dead=False)[source]¶ · classmethod from_char(char)[source]¶ · Creates a key from a character. classmethod from_dead(char)[source]¶ · Creates a dead key. classmethod from_vk(vk, **kwargs)[source]¶ ·
🌐
GitHub
github.com › moses-palmer › pynput › issues › 440
GlobalHotKeys doesn't like Arrow Keys · Issue #440 · moses-palmer/pynput
January 17, 2022 - import time from pynput import keyboard def on_q(): print("q pressed") def on_alt(): print("alt pressed") def on_up(): print("up pressed") globalHotkeys = keyboard.GlobalHotKeys({ 'q': on_q, '<alt>': on_alt, '<up>': on_up # this doesn't work. It accepts <up> as a key name, but on_up isn't called when I press the up arrow key.
Author   brisingre
🌐
Speedsheet
speedsheet.io › s › pynput
pynput SpeedSheet
pynput · Close Send · Thanks for the feedback. We appreciate it!
🌐
Read the Docs
pynput.readthedocs.io › en › latest › _modules › pynput › keyboard › _base.html
Source code for pynput.keyboard._base - Read the Docs
This is a modifier. shift_l = KeyCode.from_vk(0) #: The right Shift key. This is a modifier. shift_r = KeyCode.from_vk(0) #: The Space key. space = KeyCode.from_vk(0) #: The Tab key. tab = KeyCode.from_vk(0) #: An up arrow key. up = KeyCode.from_vk(0) #: The play/pause toggle.
🌐
Apple Community
discussions.apple.com › thread › 253390492
May I virtualization a Down arrow key pre… - Apple Community
November 21, 2021 - from pynput import keyboard from pynput.keyboard import Key, Controller keyb = Controller() is_alt_pressed = False is_shift_pressed = False is_down_pressed = False def on_press(key): global is_alt_pressed global is_shift_pressed global is_down_pressed if key == Key.shift_l: is_shift_pressed = True; if key == Key.alt_l: is_alt_pressed = True; if(is_shift_pressed and is_alt_pressed and (key == Key.up)): keyb.press(Key.down) is_down_pressed = True def on_release(key): global is_alt_pressed global is_shift_pressed global is_down_pressed if(is_down_pressed): is_down_pressed = False keyb.release(Key.down) if key == Key.shift_l: is_shift_pressed = False; if key == Key.alt_l: is_alt_pressed = False; # Collect events until released print("Keyboard macros running...") with keyboard.Listener( on_press=on_press, on_release=on_release) as listener: listener.join()
🌐
GitHub
github.com › moses-palmer › pynput › issues › 107
Some keyboard presses combination doesn't work e.g shift+arrow · Issue #107 · moses-palmer/pynput
August 13, 2018 - from pynput.keyboard import Key, Controller keyboard = Controller() with keyboard.pressed(Key.shift): keyboard.press(Key.down) keyboard.release(Key.down)
Author   karamjaber
Find elsewhere
🌐
PyPI
pypi.org › project › pynput
pynput · PyPI
This is to remove any modifier state from the key events, and to normalise modifiers with more than one physical button. The method pynput.keyboard.HotKey.parse is a convenience function to transform shortcut strings to key collections.
      » pip install pynput
    
Published   Mar 17, 2025
Version   1.8.1
🌐
Read the Docs
app.readthedocs.org › projects › pynput › downloads › pdf › latest pdf
pynput Release 1.7.6 Jul 11, 2023
A right arrow key. scroll_lock = <0> The ScrollLock key. This may be undefined for some platforms. shift = <0> A generic Shift key. This is a modifier. 2.2. Handling the keyboard · 19 · pynput, Release 1.7.6 · shift_l = <0> The left Shift key. This is a modifier.
🌐
Ubuntu
manpages.ubuntu.com › manpages › noble › man3 › pynput.3.html
Ubuntu Manpage: pynput - pynput Documentation
shift pressed while pressing arrow keys, do not work as expected. • · Index · Moses Palmér · 2015-2024, Moses Palmér 1.7.6 Feb 28, 2024 · PYNPUT(3)
🌐
Reddit
reddit.com › r/learnpython › how to interrupt user input when the up arrow key is pressed and capture the input?
r/learnpython on Reddit: How to interrupt user input when the up arrow key is pressed and capture the input?
December 19, 2022 -

I want to allow the user to cycle back through input prompts if they want. The code needs to capture whatever text was entered up to the point the up arrow key was pressed.

The code to test the concept is as follows:

import keyboard

def on_up_arrow_press(self):
keyboard.send("enter")

keyboard.on_press_key("up", on_up_arrow_press)

user_input = input("Enter something: ")
print("Input entered:", user_input)

on_up_arrow_press is called asynchronously when the up arrow key is pressed. the enter key is then sent to the terminal.

the problem is that user_input is not correctly capturing what the user typed. I'm either getting nothing or weirdly, I'm getting text that was input previously.

Is there a way I can correct the code to ensure user_input is always captured correctly or is there a way to access the keyboard buffer to extract the input?

🌐
GitHub
github.com › moses-palmer › pynput › blob › master › lib › pynput › keyboard › _base.py
pynput/lib/pynput/keyboard/_base.py at master · moses-palmer/pynput
The actual interface to keyboard classes is defined here, but the · implementation is located in a platform dependent module. """ · # pylint: disable=R0903 · # We implement stubs · · import contextlib · import enum · import threading · import unicodedata · · import six · · from pynput._util import AbstractListener, prefix ·
Author   moses-palmer
🌐
Stack Overflow
stackoverflow.com › questions › 53254207 › pynput-module-right-key-press-not-working
python - pynput module right key press not working? - Stack Overflow
November 11, 2018 - def enableMobileHotspot(): keyboard = Controller() #Open Settings print("Opening Settings") keyboard.press(Key.cmd) #Key.cmd = Windows Key keyboard.press("i") keyboard.release(Key.cmd) keyboard.release("i") time.sleep(3) #Navigate to Network & Internet print("Navigating to Network & Internet") keyboard.press(Key.tab) keyboard.release(Key.tab) time.sleep(0.3) keyboard.press(Key.right) time.sleep(0.3) keyboard.release(Key.right) time.sleep(0.3) keyboard.press(Key.right) keyboard.release(Key.right) time.sleep(0.3) keyboard.press(Key.right) keyboard.release(Key.right) time.sleep(0.3) keyboard.pres
🌐
pytz
pythonhosted.org › pynput › _modules › pynput › keyboard › _base.html
pynput.keyboard._base — pynput 1.1.2 documentation
down = 0 #: The End key. end = ... f10 = 0 f11 = 0 f12 = 0 f13 = 0 f14 = 0 f15 = 0 f16 = 0 f17 = 0 f18 = 0 f19 = 0 f20 = 0 #: The Home key. home = 0 #: A left arrow key....
Top answer
1 of 2
1

When I tried something like:

% cat test.py
char = raw_input()
print("\nInput char is [%s]." % char)
% python a.py
^[[A
           ].

It blanked out the "\Input char is [" part of the print statement. It appears that raw_input() does not receive escaped characters. The terminal program is catching the escaped keystrokes and using it to manipulate the screen. You will have to use a lower level program to catch these characters. Check out if Finding the Values of the Arrow Keys in Python: Why are they triples? help on how to get these keystrokes.

From the currently accepted answer:

    if k=='\x1b[A':
            print "up"
    elif k=='\x1b[B':
            print "down"
    elif k=='\x1b[C':
            print "right"
    elif k=='\x1b[D':
            print "left"
    else:
            print "not an arrow key!"
2 of 2
1

I think you're looking for pynput.keyboard.Listener, which allows you to monitor the keyboard and to take different actions depending on the key that is pressed. It is available for Python 2.7.

This example is a good way to get started:

from pynput import keyboard

def on_press(key):
    try:
        print('alphanumeric key {0} pressed'.format(
            key.char))
    except AttributeError:
        print('special key {0} pressed'.format(
            key))

def on_release(key):
    print('{0} released'.format(
        key))
    if key == keyboard.Key.esc:
        # Stop listener
        return False

# Collect events until released
with keyboard.Listener(
        on_press=on_press,
        on_release=on_release) as listener:
    listener.join()

# ...or, in a non-blocking fashion:
listener = keyboard.Listener(
    on_press=on_press,
    on_release=on_release)
listener.start()
🌐
Nitratine
nitratine.net › blog › post › simulate-keypresses-in-python
Simulate Keypresses In Python - Nitratine
December 16, 2017 - This demonstrates how to press keys with Python. Using pynput we are able to simulate key presses into any window. This will show you how to press and release a key, type special keys and type a sentence.
🌐
GitHub
github.com › moses-palmer › pynput › blob › master › CHANGES.rst
pynput/CHANGES.rst at master · moses-palmer/pynput
Corrected handling of some special keys, including arrow keys, when combined with modifiers on Windows.
Author   moses-palmer
🌐
CopyProgramming
copyprogramming.com › howto › how-to-track-and-simulate-arrow-keys-through-pynput-in-python
Simulating and Tracking Arrow Keys in Python using Pynput - Python 3 x
June 6, 2023 - This is functional, though I am unsure of the name for its use with arrow keys. ... from pynput.keyboard import Key, Controller kb = Controller() kb.press(Key.up) # Presses "up" key kb.release(Key.up) # Releases "up" key kb.press(Key.left) # Presses "left" key kb.release(Key.left) #etc..