如何以编程方式在 Python 中按下控制键和右箭头键?

我需要使用键盘包以编程方式同时按下控制键和右箭头键。


我试过


keyboard.press("ctrl+right")

keyboard.press("ctrl+\033[C")

但两者都会导致ValueError:


Traceback (most recent call last):

  File "C:\Users\User\anaconda3\lib\site-packages\pyHook\HookManager.py", line 351, in KeyboardSwitch

    return func(event)

  File "test2.py", line 37, in onKeyboardEvent

    keyboard.press("ctrl+\033[C")

  File "C:\Users\User\anaconda3\lib\site-packages\pynput\keyboard\_base.py", line 362, in press

    resolved = self._resolve(key)

  File "C:\Users\User\anaconda3\lib\site-packages\pynput\keyboard\_base.py", line 556, in _resolve

    raise ValueError(key)

ValueError: ctrl+[C

如何发送Ctrl-Right Arrow击键?


MMMHUHU
浏览 134回答 1
1回答

冉冉说

我尝试运行keyboard.press("ctrl+right")有效(Windows),您可能会在按下后忘记释放该键。尝试keyboard.press_and_release("ctrl+right")
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python