Skip to main content
 首页 » 编程设计

python之用pyusb控制鼠标

2025年05月04日7xxx_UU

我必须制作一个应用程序,执行以下操作:

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

我已经尝试过 pyusb,但我从未找到任何 3 个问题的示例。
有什么想法吗?

请您参考如下方法:

我对 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 线程中描述的小部件:Set cursor position in a Text widget .

要禁用鼠标,您可以查看this post。并调整代码以禁用鼠标而不是触摸板(但帖子提供了一些有趣的键作为开始)。