Linux

xbindkeys 綁定游標向右或向左鍵按下

  • July 23, 2022

我想對我的滑鼠進行程式,以便每當游標向右或向左移動時,作業系統都會將其解釋為按下右鍵或按下左鍵。哪些滑鼠事件對應於左右游標,以及在 xbindkeys 中使用的語法是什麼(因為這似乎是正確的工具)。

使用xev -event mouseI see the cursor move 會產生 MotionNotify 事件 - 會以某種方式導致該事件的接收導致右/左鍵按下?

這個使用pynput包的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)

引用自:https://unix.stackexchange.com/questions/710960