如何在 python 中通过 pynput 跟踪和模拟箭头键
How to track and simulate arrow keys through pynput in python
所以我正在开发一个需要箭头键按下的程序 simulated/emulated?:
keyboard.press(Key.ctrl)
keyboard.press(Key.alt)
keyboard.press(Key.delete)
keyboard.release(Key.ctrl)
keyboard.release(Key.alt)
keyboard.release(Key.delete)
这行得通,但我不知道如何调用箭头键
(在 win 10 上)
在 python 中,您可以使用 dir
函数查看枚举值。
from pynput import keyboard
print(dir(keyboard.Key)) # show full enum list
输出
['__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']
可以看到上、下、左、右 在列表中。试试这些键。
如何按下所有这四个键:
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)
如果你需要多次使用它,你可以通过创建一个函数来简化它:
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.
希望我有所帮助。
所以我正在开发一个需要箭头键按下的程序 simulated/emulated?:
keyboard.press(Key.ctrl)
keyboard.press(Key.alt)
keyboard.press(Key.delete)
keyboard.release(Key.ctrl)
keyboard.release(Key.alt)
keyboard.release(Key.delete)
这行得通,但我不知道如何调用箭头键
(在 win 10 上)
在 python 中,您可以使用 dir
函数查看枚举值。
from pynput import keyboard
print(dir(keyboard.Key)) # show full enum list
输出
['__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']
可以看到上、下、左、右 在列表中。试试这些键。
如何按下所有这四个键:
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)
如果你需要多次使用它,你可以通过创建一个函数来简化它:
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.
希望我有所帮助。