小编The*_*ive的帖子

Python 检查鼠标是否被点击

所以我试图用 Python 构建一个简短的脚本。我想要做的是,如果单击鼠标,鼠标将重置到某个任意位置(现在是屏幕中间)。我希望它在后台运行,因此它可以在操作系统(很可能是 Chrome 或某些网络浏览器)中运行。我也希望用户可以按住某个按钮(比如 ctrl),然后他们可以点击离开而不重置位置。这样他们就可以毫无挫折地关闭脚本。

我很确定我知道如何做到这一点,但我不确定要使用哪个库。我更喜欢它是跨平台的,或者至少是 Windows + Mac。到目前为止,这是我的代码:

#! python3
# resetMouse.py - resets mouse on click - usuful for students with
# cognitive disabilities.

import pymouse

width, height = m.screen_size()
midWidth = (width + 1) / 2
midHeight = (height + 1) / 2

m = PyMouse()
k = PyKeyboard()


def onClick():
    m.move(midWidth, midHeight)


try:
    while True:
        # if button is held down:
            # continue
        # onClick()
except KeyboardInterrupt:
    print('\nDone.')
Run Code Online (Sandbox Code Playgroud)

python mouseevent keyboard-events

5
推荐指数
2
解决办法
2万
查看次数

标签 统计

keyboard-events ×1

mouseevent ×1

python ×1