【问题标题】:controlling mouse with pyusb用pyusb控制鼠标
【发布时间】:2011-09-22 12:09:30
【问题描述】:

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

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

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

【问题讨论】:

  • 你至少应该指定它是什么操作系统(我猜是 Linux,但你应该告诉我们)和什么环境(我猜是 Xorg,但你应该告诉我们)。

标签: python usb mouse pyusb


【解决方案1】:

我不太了解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 并修改代码以禁用鼠标而不是触摸板(但帖子提供了一些有趣的键开始)。

【讨论】:

    猜你喜欢
    • 2021-10-30
    • 2010-11-13
    • 2011-05-04
    • 1970-01-01
    • 2020-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多