xbindkeys 绑定光标移动到右或左按键

xbindkeys 绑定光标移动到右或左按键

我想对我的鼠标进行编程,以便每当光标向右或向左移动时,操作系统都会将其解释为按右键或按左键。哪些鼠标事件对应于光标左右,以及 xbindkeys 中使用的语法是什么(因为这似乎是正确的工具)。

使用xev -event mouse我看到光标移动给出了 MotionNotify 事件 - 可以以某种方式导致接收到此事件导致右/左键按下吗?

答案1

这个Python脚本使用平输入包达到了预期的目标。

import time
from pynput import mouse, keyboard
from pynput.keyboard import Key, Controller

keyboard = Controller()

prevX = -1
def on_move(x, y):
  global prevX
  if prevX == -1:
    prevX = x
  elif prevX < x:
    keyboard.press(Key.right)
    keyboard.release(Key.right)
  elif prevX > x:
    keyboard.press(Key.left)
    keyboard.release(Key.left)
  if prevX != x:
    prevX = x

listener = mouse.Listener(
  on_move=on_move)
listener.daemon = True
listener.start()

while True:
  time.sleep(1)

调整为仅在某个方向上发生确定数量的鼠标事件后才触发按键:

import time
from pynput import mouse, keyboard
from pynput.keyboard import Key, Controller

keyboard = Controller()

sensitivity = 20 # number of events to trigger key press
prevX = -1
leftEvents = 0
rightEvents = 0
def on_move(x, y):
  global prevX
  global leftEvents
  global rightEvents

  if prevX == -1:
    prevX = x
  elif prevX < x:
    rightEvents += 1
    leftEvents = 0
  elif prevX > x:
    leftEvents += 1
    rightEvents = 0
  if rightEvents == sensitivity:
    keyboard.press(Key.right)
    keyboard.release(Key.right)
    rightEvents = 0
  if leftEvents == sensitivity:
    keyboard.press(Key.left)
    keyboard.release(Key.left)
    leftEvents = 0
  if prevX != x:
    prevX = x

listener = mouse.Listener(
  on_move=on_move)
listener.daemon = True
listener.start()

while True:
  time.sleep(1)

相关内容