简体   繁体   English

如何在 Win 上不移动鼠标的情况下发送点击事件?

[英]How do I send a click event without moving my mouse on Win?

I'm trying to write a function to allow me to send a left click to x, y co-ordinates on screen without my mouse cursor moving.我正在尝试编写一个函数,允许我在不移动鼠标光标的情况下向屏幕上的x, y坐标发送左键。

I've read through the send message documentation and the mouse input notification documentation and I've come up with a few different approaches, none of which work.我已经通读了发送消息文档鼠标输入通知文档,并提出了几种不同的方法,但都不起作用。 And none throw an error.并且没有人抛出错误。

I'm using win32gui.FindWindow to get the hwnd and then I've tried using both PostMessage and SendMessage to perform the click, none so far work.我正在使用win32gui.FindWindow来获取hwnd ,然后我尝试使用PostMessageSendMessage来执行点击,到目前为止还没有工作。

import win32api, win32gui, win32con

def control_click(x, y):

    hWnd = win32gui.FindWindow(None, "Pixel Starships")
    l_param = win32api.MAKELONG(x, y)

    if button == 'left':
        win32gui.PostMessage(hWnd, win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON, l_param)
        win32gui.PostMessage(hWnd, win32con.WM_LBUTTONUP, win32con.MK_LBUTTON, l_param)
   
control_click(526, 694)

def click(x, y):
    hWnd = win32gui.FindWindow(None, "Pixel Starships")
    lParam = win32api.MAKELONG(x, y)

    win32api.SendMessage(hWnd, win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON, lParam)
    win32api.SendMessage(hWnd, win32con.WM_LBUTTONUP, win32con.MK_LBUTTON, lParam)

click(526,694)

def leftClick(pos):
    hWnd = win32gui.FindWindow(None, "Pixel Starships")
    lParam = win32api.MAKELONG(pos[0], pos[1])

    win32gui.SendMessage(hWnd, win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON, lParam) 
    win32gui.SendMessage(hWnd, win32con.WM_LBUTTONUP, 0, lParam)

leftClick([526,964])

What do I need to do to get this function to work?我需要做什么才能使此功能正常工作?

Could this work?这能行吗?

win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0)

I would also recommend trying pyautogui .我还建议尝试pyautogui It might help.它可能会有所帮助。

I would also recommend using pyautogui because afaik it has a function to generate a click at a specific position without changing current cursor location.我还建议使用 pyautogui,因为 afaik 它具有在特定位置生成点击而不更改当前光标位置的功能。

Also you probably want to have a couple of milliseconds between button-down and button-up function because windows sometimes tend to not register the second button function due to beeing to fast after the button down.此外,您可能希望在按下按钮和按下按钮功能之间有几毫秒的时间,因为 Windows 有时往往不会注册第二个按钮功能,因为按下按钮后会很快。 Additionally if this is for a game (pixel starships), you want to randomize this milliseconds waitingframe.此外,如果这是针对游戏(像素星舰),您希望随机化此毫秒等待帧。 Otherwise you might run into anti-bot checks and get dispelled/banned from the game server if pixel starships implements those.否则,如果像素星舰实现了这些,您可能会遇到反机器人检查并被游戏服务器驱散/禁止。

I have been looking for an answer to this question for over 2 years.我一直在寻找这个问题的答案超过 2 年。 I have tried all possible scripts, nothing worked (well, pywinauto works fine in a lot of cases, but with some apps, it doesn't).我已经尝试了所有可能的脚本,但没有任何效果(好吧,pywinauto 在很多情况下都可以正常工作,但对于某些应用程序却不行)。 I found something yesterday that I want to share with you.我昨天发现了一些想和你分享的东西。 It is not a script for mouse clicks, but for keystrokes.它不是用于鼠标点击的脚本,而是用于击键的脚本。 However, it might be the way to solve your (our) problem.但是,这可能是解决您(我们的)问题的方法。 I have tested it already with some apps that gave me trouble sending keystrokes, and it worked like a charm.我已经用一些应用程序对它进行了测试,这些应用程序给我发送击键带来了麻烦,它就像一个魅力。 I hope I can get it to run with mouse clicks.我希望我可以让它通过鼠标点击运行。 Keep us informed if you find something, I will also keep looking for a solution.如果您发现了什么,请随时通知我们,我也会继续寻找解决方案。

https://github.com/byt3bl33d3r/Invoke-AutoIt https://github.com/byt3bl33d3r/Invoke-AutoIt

Loads the AutoIt DLL and PowerShell assemblies into memory and executes the specified keystrokes.将 AutoIt DLL 和 PowerShell 程序集加载到内存中并执行指定的击键。

This was mainly created to bypass some application restrictions when it came to sending keystroke input via sendkeys(), sendinput() or sendmessage().这主要是为了在通过 sendkeys()、sendinput() 或 sendmessage() 发送击键输入时绕过一些应用程序限制。 eg Windows's Remote Desktop Client ;)例如 Windows 的远程桌面客户端 ;)

Maybe you can try with Pywinauto is another library similar to PyAutoGUI, but allows you to manipulate apps via her control identifier.也许您可以尝试使用 Pywinauto 是另一个类似于 PyAutoGUI 的库,但允许您通过她的控件标识符来操作应用程序。

import pywinauto
app = pywinauto.Application().connect(path='notepad.exe')
app.UntitledNotepad.MenuSelect("Edit->Replace")

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

相关问题 python - 如何在不移动鼠标的情况下执行点击? - How can I perform a click without moving the mouse in python? 如何将鼠标单击事件发送到mac osx中的窗口 - How to send mouse click event to a window in mac osx 在指定位置单击而不移动鼠标错误 - Click in specified place without moving mouse error 鼠标点击而不移动光标? - Mouse-click without moving cursor? win32:模拟点击而不模拟鼠标移动? - win32: simulate a click without simulating mouse movement? 在无头模式下单击鼠标而不移动 cursor - Mouse-click without moving cursor in headless mode 如何在没有插入鼠标 Python 的情况下单击某处? - How can I click somewhere without having a mouse plugged in with Python? 如何在第一次单击鼠标时选择一个精灵,然后在第二次单击鼠标的位置创建一个新精灵 - How do I select a sprite on a first mouse click then create a new one in the position of a second mouse click 如何让我的乌龟停止移动并结束活动 - How can I get my turtle to stop moving, and end the event 我如何发送 ajax 请求通过使用 onclick 事件没有形式封闭标签在 django - how do i send ajax request by using onclick event without form enclosed tags in django
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM