用pyusb控制鼠标

2024-05-19 03:05:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我必须申请以下事项:

  • 禁用给定的usb鼠标在屏幕上移动指针(只移动给定的鼠标,而不是所有的鼠标)。在
  • 获取鼠标指针的坐标
  • 更改鼠标指针的y坐标

我试过pyusb,但这3个问题中的任何一个我都没有找到任何例子。
有什么想法吗?在


Tags: 屏幕事项鼠标例子usbpyusb指针
1条回答
网友
1楼 · 发布于 2024-05-19 03:05:17

我还不知道pyusb,但是您可以用Tkinter(Python中最常用的GUI之一)来处理第二个问题。以下是代码示例(找到here):

# show mouse position as mouse is moved and create a hot spot

import Tkinter as tk

root = tk.Tk()

def showxy(event):
    xm = event.x
    ym = event.y
    str1 = "mouse at x=%d  y=%d" % (xm, ym)
    root.title(str1)
    # switch color to red if mouse enters a set location range
    x = 100
    y = 100
    delta = 10  # range
    if abs(xm - x) < delta and abs(ym - y) < delta:
        frame.config(bg='red')
    else:
        frame.config(bg='yellow')


frame = tk.Frame(root, bg= 'yellow', width=300, height=200)
frame.bind("<Motion>", showxy)
frame.pack()

root.mainloop()

但是,似乎您不能仅使用Tkinter更改光标位置(请参阅本文thread以获得一些解决方法)。但是,如果您试图在文本中设置位置,您可以使用这个SO-thread:Set cursor position in a Text widget中描述的小部件。在

要禁用鼠标,您可以查看一下this post并调整代码以禁用鼠标而不是touchpad(但是文章首先给出了一些有趣的键)。在

相关问题 更多 >

    热门问题