0

I would like to program my mouse such that whenever the cursor is shifted right or left, the OS interprets this as a right key press or left key press. Which mouse events correspond to cursor right and left, and what would be the syntax to use in xbindkeys (as this seems like the right tool for it).

Using xev -event mouse I see the cursor move gives MotionNotify events - can somehow cause the reception of this event to cause a right/left key press?

2
  • Would a python script that achieves the goal be good? Commented Jul 22, 2022 at 22:48
  • If compatible with Python 2.7.9, sure. I was working on a C solution using xlib functions but still stuck on it Commented Jul 22, 2022 at 22:52

1 Answer 1

2

This python script using the pynput package achieves the desired goal.

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) 

Adjusted to trigger a key press only after a determined number of mouse events on a direction:

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) 
2
  • 1
    Thank you! While it does work, the precision is a bit too fine-grained at the moment, making the right-key and left-key events go crazy :) . Ideally it would only respond to very clear swipes right or left, i.e. if we have 10 move right events in a row or something. Commented Jul 23, 2022 at 0:16
  • 1
    You're welcome! I edited the answer with this in mind. Adjust sensitivity to your desired value :) Commented Jul 23, 2022 at 0:32

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.