Saturday 30 December 2023

How to convert absolute touch input to middle mouse button click and drags?

I bought StaffPad but unfortunately i don't have MS device to write on and use the benefits of the software. So writing with mouse on pc isn't a comfortable experience. I tried using spacedesk on my phone to try write with my capacitive stylus, but didn't work. when i tried writing the software thought that its a drag input. But I noticed that I can use my mouse's scroll wheel button to write on that software. So I'm trying to figure out a way to convert space desk's absolute touch input to middle mouse button (scroll wheel) click/drag to write in staffpad.

I tried approaching by this way:

# touch_to_middle_click_and_drag.py

import pyautogui
from pynput import mouse

# Variables to store the previous touch position
prev_x, prev_y = None, None

# Flag to track whether the middle mouse button is currently pressed
middle_button_pressed = False

def on_touch(x, y):
    global prev_x, prev_y

    if middle_button_pressed:
        # Calculate the movement since the previous position
        dx, dy = x - prev_x, y - prev_y
        pyautogui.moveRel(dx, dy)

    # Update the previous position
    prev_x, prev_y = x, y

def on_touch_press(x, y, button, pressed):
    global middle_button_pressed

    if pressed and button == mouse.Button.middle:
        # Simulate a middle mouse button press
        middle_button_pressed = True
        pyautogui.mouseDown(button='middle')

def on_touch_release(x, y, button, pressed):
    global middle_button_pressed

    if not pressed and button == mouse.Button.middle:
        # Simulate a middle mouse button release
        middle_button_pressed = False
        pyautogui.mouseUp(button='middle')

# Start listening for touch events
with mouse.Listener(on_move=on_touch, on_click=on_touch_press) as listener:
    listener.join()

I expected it to work as desired i.e. take absolute touch input and convert to scroll wheel button click and thus enabling me to write in staffpad. But its still taking dragging input when i try writing on my phone with spacedesk.



from How to convert absolute touch input to middle mouse button click and drags?

No comments:

Post a Comment