简体   繁体   English

是否可以用 python 检测鼠标是否被按住

[英]Is it possible to detect if the mouse is held down with python

Does anyone know if its possible to detect if the cursor is being held down with python?有谁知道是否可以检测到 cursor 是否被 python 按住? I was thinking something like this:我在想这样的事情:

while mouseDown == True:
    # some other code

I am quite new to python, so I apologise if I missed a really obvious solution.我对 python 很陌生,所以如果我错过了一个非常明显的解决方案,我深表歉意。

pynput works very well for mouse and keyboard automation. pynput非常适合鼠标和键盘自动化。 This simple script should help you get started:这个简单的脚本应该可以帮助您入门:

from pynput.mouse import Listener

def on_move(x, y):
    pass

def on_click(x, y, button, pressed):
    if pressed:
        # Your code here

def on_scroll(x, y, dx, dy):
    pass

with Listener(on_move=on_move, on_click=on_click, on_scroll=on_scroll) as listener:
    listener.join()

You can use pynput module to do it.您可以使用pynput模块来做到这一点。 It can be installed using pip command pip install pynput .它可以使用 pip 命令pip install pynput Also, see the documentation to understand the full functionality of pynput .此外,请参阅文档以了解pynput的全部功能。 Basically it is used to log key input of keyboard and mouse.基本上它用于记录键盘和鼠标的键输入。

Here is how you can check if the mouse key is held down or not.这是检查鼠标键是否被按住的方法。

from pynput.mouse import Listener

# This function will be called when any key of mouse is pressed
def on_click(*args):
    # see what argument is passed.
    print(args)
    if args[-1]:
        # Do something when the mouse key is pressed.
        print('The "{}" mouse key has held down'.format(args[-2].name))

    elif not args[-1]:
        # Do something when the mouse key is released.
        print('The "{}" mouse key is released'.format(args[-2].name))

# Open Listener for mouse key presses
with Listener(on_click=on_click) as listener:
    # Listen to the mouse key presses
    listener.join()

You can use pygame to handle events like these:您可以使用 pygame 来处理如下事件:

import pygame

(width, height) = (400, 250)
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('Title')
screen.fill((255,255,255))
pygame.display.flip()    

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.MOUSEBUTTONDOWN:
            # your code here
        if event.type == pygame.QUIT:
            running = False

You can also control the number of times pygame checks if the mouse button is pressed down (called FPS if you are not familiar with game terminology):您还可以控制 pygame 检查鼠标按钮是否按下的次数(如果您不熟悉游戏术语,则称为 FPS):

import pygame

(width, height) = (400, 250)
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('Title')
screen.fill((255,255,255))
pygame.display.flip()    

running = True
clock = pygame.time.Clock()
while running:
    for event in pygame.event.get():
        if event.type == pygame.MOUSEBUTTONDOWN:
            # your code here
        if event.type == pygame.QUIT:
            running = False
    clock.tick(30) # capped at 30 fps

Does anyone know if its possible to detect if the cursor is being held down with python?有谁知道是否可以检测到 cursor 是否被 python 按住?

A mouse is nothing more than an input device.鼠标只不过是一种输入设备。 Depending on the operating system and its configuration you need to see how the input/output events are processed.根据操作系统及其配置,您需要了解如何处理输入/输出事件。

This is the job of the a https://en.wikipedia.org/wiki/Display_server on *nix system.这是 *nix 系统上的https://en.wikipedia.org/wiki/Display_server的工作。

If you target a specific operating system then you need to check the documentation (on *nix systems you are probably using x.org or wayland on windows on mac you need to check their docs)如果您针对特定的操作系统,那么您需要检查文档(在 *nix 系统上,您可能在 Mac 上的 windows 上使用 x.org 或 wayland,您需要检查他们的文档)

If you need this working everywhere you need to create the boilerplate code that identifies the operating system and gets the event.如果您需要在任何地方工作,您需要创建识别操作系统并获取事件的样板代码。 In *nix you could theoritically start reading from /dev but its better if you use the native interfaces given to you在 *nix 中,理论上您可以从/dev开始阅读,但如果您使用提供给您的本机接口,效果会更好

Or the best thing would be to just find a library that does all this for you, https://pypi.org/project/pynput/ looks like a good candidate.或者最好的办法就是找到一个为您完成所有这些工作的库, https://pypi.org/project/pynput/看起来是个不错的选择。

I've been looking for this one and found this easier code to implement.我一直在寻找这个,发现这个更容易实现的代码。

import win32api

while True:
    if win32api.GetKeyState(0x01)<0: #if mouse left button is pressed
       print("Pressed")
    else: #if mouse left button is not pressed
       print("Released")

This allows you to check in real time if mouse left is held or not.这使您可以实时检查是否按住鼠标左键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM