【问题标题】:How to track and simulate arrow keys through pynput in python如何通过python中的pynput跟踪和模拟箭头键
【发布时间】:2021-01-19 16:25:35
【问题描述】:

所以我正在开发一个需要模拟/模拟箭头键按下的程序?:

keyboard.press(Key.ctrl)
keyboard.press(Key.alt)
keyboard.press(Key.delete)
keyboard.release(Key.ctrl)
keyboard.release(Key.alt)
keyboard.release(Key.delete)

这可行,但我不知道如何调用箭头键

(在赢 10 上)

【问题讨论】:

    标签: python-3.x windows keyboard pynput


    【解决方案1】:

    在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']
    

    您可以在列表中看到。试试这些键。

    【讨论】:

      【解决方案2】:

      如何按下所有四个键:

      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.
      

      希望我能帮上忙。

      【讨论】:

        猜你喜欢
        • 2011-09-07
        • 2013-04-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多