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 OverflowHow 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.
In python, you can view enum values using the dir function.
from pynput import keyboard
print(dir(keyboard.Key)) # show full enum list
Output
['__class__', '__doc__', '__members__', '__module__', 'alt', 'alt_l', 'alt_r',
'backspace', 'caps_lock', 'cmd', 'cmd_r', 'ctrl', 'ctrl_l', 'ctrl_r', 'delete',
'down', 'end', 'enter', 'esc', 'f1', 'f10', 'f11', 'f12', 'f13', 'f14', 'f15',
'f16', 'f17', 'f18', 'f19', 'f2', 'f20', 'f3', 'f4', 'f5', 'f6', 'f7', 'f8', 'f9',
'home', 'insert', 'left', 'media_next', 'media_play_pause', 'media_previous',
'media_volume_down', 'media_volume_mute', 'media_volume_up', 'menu', 'num_lock',
'page_down', 'page_up', 'pause', 'print_screen', 'right', 'scroll_lock', 'shift',
'shift_r', 'space', 'tab', 'up']
You can see up, down, left, right in the list. Try those keys.
GlobalHotKeys doesn't like Arrow Keys
May I virtualization a Down arrow key pre… - Apple Community
Some keyboard presses combination doesn't work e.g shift+arrow
How to interrupt user input when the up arrow key is pressed and capture the input?
» pip install pynput
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?
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!"
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()