如何将绝对触摸输入转换为鼠标中键单击和拖动?

问题描述 投票:0回答:1

我购买了StaffPad,但不幸的是我没有可以在其上书写和使用该软件的优势的 MS 设备。因此,在电脑上用鼠标书写并不是一种舒适的体验。我尝试在手机上使用 spacedesk 尝试用电容笔进行书写,但没有成功。当我尝试编写该软件时,我认为它是一个拖动输入。但我注意到我可以使用鼠标的滚轮按钮在该软件上书写。所以我试图找到一种方法,将空间桌面的绝对触摸输入转换为鼠标中键(滚轮)单击/拖动以在工作簿中书写。

我尝试通过这种方式接近:

# 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()

我希望它能够按预期工作,即采用绝对触摸输入并转换为滚轮按钮单击,从而使我能够在记事本中书写。但当我尝试使用 spacedesk 在手机上书写时,它仍然需要拖动输入。

python windows performance model-view-controller type-conversion
1个回答
0
投票

您希望将触摸输入转换为鼠标中键单击和拖动,以便更舒适地使用 StaffPad。您需要监听触摸输入并将其转换为鼠标中键操作。

尝试定义一个

on_move()
函数来跟踪触摸输入的移动。每当触摸输入移动时就会调用此函数。它将计算自前一个位置以来的移动,并使用
pyautogui.moveRel()
.

相应地移动鼠标光标。

添加

touch_active
标志来跟踪触摸输入是否处于活动状态。当按下鼠标中键时,该标志将设置为
True
;当释放鼠标中键时,该标志将设置为
False
。该标志将在
on_move()
函数中使用来确定是否移动鼠标光标。

# touch_to_middle_click_and_drag.py

import pyautogui
from pynput.mouse import Listener, Button

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

# Flag to track whether a touch input is active
touch_active = False


def on_move(x, y):
    global prev_x, prev_y, touch_active

    if touch_active:
        # 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_click(x, y, button, pressed):
    global touch_active

    if button == Button.middle:
        if pressed:
            # Start tracking touch as middle mouse drag
            touch_active = True
            pyautogui.mouseDown(x, y, button='middle')
        else:
            # Stop tracking touch
            touch_active = False
            pyautogui.mouseUp(button='middle')


# Start listening for mouse events
with Listener(on_move=on_move, on_click=on_click) as listener:
    listener.join()
© www.soinside.com 2019 - 2024. All rights reserved.