簡體   English   中英

在指定位置單擊而不移動鼠標錯誤

[英]Click in specified place without moving mouse error

所以我一直在尋找多種方法來執行“點擊”而不實際移動鼠標。 經過數小時的搜索,我找到了這兩個頁面:ctypes mouse_eventshttps://schurpf.com/python-mouse-control/其中有一些代碼顯然可以在不移動鼠標的情況下執行點擊。 代碼似乎來自同一個人,但https://schurpf.com/python-mouse-control/似乎更符合“添加的功能”。 我嘗試修復它一段時間,但沒有得到任何地方,並堅持使用以下腳本

import win32gui, win32api, win32con, ctypes

class Mouse:
    """It simulates the mouse"""
    MOUSEEVENTF_MOVE = 0x0001 # mouse move
    MOUSEEVENTF_LEFTDOWN = 0x0002 # left button down
    MOUSEEVENTF_LEFTUP = 0x0004 # left button up
    MOUSEEVENTF_RIGHTDOWN = 0x0008 # right button down
    MOUSEEVENTF_RIGHTUP = 0x0010 # right button up
    MOUSEEVENTF_MIDDLEDOWN = 0x0020 # middle button down
    MOUSEEVENTF_MIDDLEUP = 0x0040 # middle button up
    MOUSEEVENTF_WHEEL = 0x0800 # wheel button rolled
    MOUSEEVENTF_ABSOLUTE = 0x8000 # absolute move
    SM_CXSCREEN = 0
    SM_CYSCREEN = 1

    def _do_event(self, flags, x_pos, y_pos, data, extra_info):
        """generate a mouse event"""
        x_calc = 65536 * x_pos / ctypes.windll.user32.GetSystemMetrics(self.SM_CXSCREEN) + 1
        y_calc = 65536 * y_pos / ctypes.windll.user32.GetSystemMetrics(self.SM_CYSCREEN) + 1
        return ctypes.windll.user32.mouse_event(flags, x_calc, y_calc, data, extra_info)

    def _get_button_value(self, button_name, button_up=False):
        """convert the name of the button into the corresponding value"""
        buttons = 0
        if button_name.find("right") >= 0:
            buttons = self.MOUSEEVENTF_RIGHTDOWN
        if button_name.find("left") >= 0:
            buttons = buttons + self.MOUSEEVENTF_LEFTDOWN
        if button_name.find("middle") >= 0:
            buttons = buttons + self.MOUSEEVENTF_MIDDLEDOWN
        if button_up:
            buttons = buttons << 1
        return buttons

    def move_mouse(self, pos):
        """move the mouse to the specified coordinates"""
        (x, y) = pos
        old_pos = self.get_position()
        x =  x if (x != -1) else old_pos[0]
        y =  y if (y != -1) else old_pos[1]
        self._do_event(self.MOUSEEVENTF_MOVE + self.MOUSEEVENTF_ABSOLUTE, x, y, 0, 0)

    def press_button(self, pos=(-1, -1), button_name="left", button_up=False):
        """push a button of the mouse"""
        self.move_mouse(pos)
        self._do_event(self.get_button_value(button_name, button_up), 0, 0, 0, 0)

    def click(self, pos=(-1, -1), button_name= "left"):
        """Click at the specified placed"""
##        self.move_mouse(pos)
        self._do_event(self._get_button_value(button_name, False)+self._get_button_value(button_name, True), 0, 0, 0, 0)

    def double_click (self, pos=(-1, -1), button_name="left"):
        """Double click at the specifed placed"""
        for i in xrange(2):
            self.click(pos, button_name)

    def get_position(self):
        """get mouse position"""
        return win32api.GetCursorPos()
#-----------------------------------------------------------------------------------------
#Added functions
#-----------------------------------------------------------------------------------------
    def invisible_click(self,pos=(-1, -1), button_name="left"):
        """Click in specified place without moving mouse"""
        xcur,ycur = win32gui.GetCursorPos()
        ctypes.windll.user32.SetCursorPos(pos[0],pos[1])
        self.click(pos,button_name)
        ctypes.windll.user32.SetCursorPos(xcur,ycur)

    def invisible_click_rel(self,handle,pos, button_name="left"):
        """Click in window coordinates without moving mouse"""
        #get window info
        xleft, ytop, xright, ybottom = win32gui.GetWindowRect(handle)

        xcur,ycur = win32gui.GetCursorPos()

        ctypes.windll.user32.SetCursorPos(pos[0]+xleft,pos[1]+ytop)
        self.click((pos[0]+xleft,pos[1]+ytop),button_name)
        ctypes.windll.user32.SetCursorPos(xcur,ycur)

if __name__ == '__main__':
    p = (500,500)
    print (win32gui.GetForegroundWindow())
    mouse = Mouse()
    mouse.invisible_click(p)

我想要做的是觸發鼠標 class 的 invisible_click function,以便它在 position 500,500 處執行“隱形點擊”。

我用上面的代碼得到的錯誤是

File "C:\Users\MyName\Desktop\test1del\Future.py", line 21, in _do_event
    return ctypes.windll.user32.mouse_event(flags, x_calc, y_calc, data, extra_info)
ctypes.ArgumentError: argument 2: <class 'TypeError'>: Don't know how to convert parameter 2

抱歉,如果我遺漏了一些明顯的東西,我將非常感謝您的幫助! 謝謝閱讀

文件“C:\Users\MyName\Desktop\test1del\Future.py”,第 21 行,在 _do_event 返回 ctypes.windll.user32.mouse_event(flags, x_calc, y_calc, data, extra_info) ctypes.ArgumentError: argument 2: :不知道如何轉換參數2

此錯誤信息表明function mouse_event中傳入的參數x_calcy_calc類型錯誤。 它們是float ,但需要unsigned int

試試下面的代碼:

import math
#...
    x_calc = math.floor(65536 * x_pos / ctypes.windll.user32.GetSystemMetrics(self.SM_CXSCREEN) + 1)
    y_calc = math.floor(65536 * y_pos / ctypes.windll.user32.GetSystemMetrics(self.SM_CYSCREEN) + 1)

注意: mouse_event function 已被取代。 請改用SendInput

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM