游戏中的Python鼠标点击(直接输入)

2 人关注

我搜索了很多关于模拟directx游戏的鼠标点击和移动的资料。我找到了一些关于按键的好资料,但没有关于鼠标的资料。实际上,关于直接输入的按键,在stackoverflow上有一个很好的主题( 模拟Python的按键来控制游戏 ).但我没有足够的经验来使它在某些位置上的鼠标点击工作。

我试过很多Python模块,如pyautogui、win32等,但它们都不工作。甚至我试着在自动键盘上点击它,并将参数发送到'.ahk'文件,但这并不稳定,也不是一个好方法。我将感谢每一个评论。我在这上面工作了2天,我完全迷失了。谢谢!

我从reddit上找到了这个代码,用于游戏中的点击,但它也没有用。

import ctypes
PUL = ctypes.POINTER(ctypes.c_ulong)
class KeyBdInput(ctypes.Structure):
    _fields_ = [("wVk", ctypes.c_ushort),
                ("wScan", ctypes.c_ushort),
                ("dwFlags", ctypes.c_ulong),
                ("time", ctypes.c_ulong),
                ("dwExtraInfo", PUL)]
class HardwareInput(ctypes.Structure):
    _fields_ = [("uMsg", ctypes.c_ulong),
                ("wParamL", ctypes.c_short),
                ("wParamH", ctypes.c_ushort)]
class MouseInput(ctypes.Structure):
    _fields_ = [("dx", ctypes.c_long),
                ("dy", ctypes.c_long),
                ("mouseData", ctypes.c_ulong),
                ("dwFlags", ctypes.c_ulong),
                ("time", ctypes.c_ulong),
                ("dwExtraInfo", PUL)]
class Input_I(ctypes.Union):
    _fields_ = [("ki", KeyBdInput),
                ("mi", MouseInput),
                ("hi", HardwareInput)]
class Input(ctypes.Structure):
    _fields_ = [("type", ctypes.c_ulong),
                ("ii", Input_I)]
def set_pos(x, y):
    x = 1 + int(x * 65536./1920.)
    y = 1 + int(y * 65536./1080.)
    extra = ctypes.c_ulong(0)
    ii_ = Input_I()
    ii_.mi = MouseInput(x, y, 0, (0x0001 | 0x8000), 0, ctypes.pointer(extra))
    command = Input(ctypes.c_ulong(0), ii_)
    ctypes.windll.user32.SendInput(1, ctypes.pointer(command), ctypes.sizeof(command))
def left_click():
    extra = ctypes.c_ulong(0)
    ii_ = Input_I()
    ii_.mi = MouseInput(0, 0, 0, 0x0002, 0, ctypes.pointer(extra))
    x = Input(ctypes.c_ulong(0), ii_)
    ctypes.windll.user32.SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))
    extra = ctypes.c_ulong(0)
    ii_ = Input_I()
    ii_.mi = MouseInput(0, 0, 0, 0x0004, 0, ctypes.pointer(extra))
    x = Input(ctypes.c_ulong(0), ii_)
    ctypes.windll.user32.SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))

left_click()函数是有效的,但click在所有模块中都有效,我需要的是set_pos()函数,但不幸的是它没有。

python
directx
directinput
sword1st
sword1st
发布于 2019-03-02
3 个回答
sword1st
sword1st
发布于 2020-04-06
已采纳
0 人赞同

我做了很多研究,发现了pyautoit模块。它真的很棒,很容易使用。我认为它只适用于32位的Python,我不能把它安装到64位的版本。你也应该安装autoitx3,但我不确定它是否能在一个dll上工作。安装了autoitx3和pyautoit模块后,你可以使用这个示例代码。

import autoit
import time
time.sleep(2)
#"left" stand for left click, 1243 and 1035 is x and y coordinates and 1 is number of clicks.
autoit.mouse_click("left", 1243, 1035, 1)
    
你是如何安装autoitx3 dll的,又是在哪里安装的?我只用了autoit,但我得到了同样的问题。
stevecody
stevecody
发布于 2020-04-06
0 人赞同

对于Directx游戏,你必须自我测试一下,如果直接输入确实起作用。

但是通过pywinauto软件包,你可以模拟鼠标点击和鼠标移动。

看一看 pywinauto.mouse

要安装pywinauto软件包,你可以使用这个bat文件,为python 27安装。

install-Pywinauto.bat

C:\Python27\scripts\pip.exe install pywinauto
pause

现在你已经准备好了。

Left-mouse-click.py

import pywinauto
pywinauto.mouse.click(button='left', coords=(0, 0)) 

Double-Left-Mouse-Click.py

import pywinauto
pywinauto.mouse.double_click(button='left', coords=(0, 0))

而如果你把它和AutoPytonLauncher

你可以在你的桌面上点击一个3D按钮图像来发送任何键盘快捷键 宏的 or MouseClicks or MouseMovements到Windows应用程序或游戏。(在不失去焦点的情况下,活动窗口。)

谢谢你的评论,但它没有工作。它只是在没有游戏的情况下工作。在游戏中,它不能点击给定的坐标。
好的,你能说说在哪个游戏中不工作,你是否在AutoPythonLauncher中尝试过(它可以在不关注Windows的情况下发送/执行)。- 注意:如果还是不行,那就是Game Maker阻止了通过命令来使用鼠标坐标移动,那么它就被Game Self所取代。(也许你可以尝试做一个一步一步的宏动作,例如:getmousePosition + then true 发送一个键盘快捷键宏,发送10xUp(key)+10xLeft(key)
然后,如果鼠标指针到达该坐标,也许你可以尝试用键盘快捷键来模拟鼠标点击,例如:发送1xEnter(键)。
Bandoh
Bandoh
发布于 2020-04-06
0 人赞同
def move(x=None, y=None, duration=0.25, absolute=True, interpolate=False, **kwargs):
    if (interpolate):
        print("mouse move {}".format(interpolate))
        current_pixel_coordinates = win32api.GetCursorPos()
        if interpolate:
            current_pixel_coordinates = win32api.GetCursorPos()
            start_coordinates = _to_windows_coordinates(*current_pixel_coordinates)
            end_coordinates = _to_windows_coordinates(x, y)
            print("In interpolate")
            coordinates = _interpolate_mouse_movement(
                start_windows_coordinates=start_coordinates,
                end_windows_coordinates=end_coordinates
            print(coordinates)
        else:
            coordinates = [end_coordinates]
        for x, y in coordinates:
            extra = ctypes.c_ulong(0)
            ii_ = Input_I()
            ii_.mi = MouseInput(x, y, 0, (0x0001 | 0x8000), 0, ctypes.pointer(extra))
            x = Input(ctypes.c_ulong(0), ii_)
            ctypes.windll.user32.SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))
            time.sleep(duration / len(coordinates))
    else:
        x = int(x)
        y = int(y)
        coordinates = _interpolate_mouse_movement(
            start_windows_coordinates=(0, 0),
            end_windows_coordinates=(x, y)
        for x, y in coordinates:
            extra = ctypes.c_ulong(0)
            ii_ = Input_I()
            ii_.mi = MouseInput(x, y, 0, 0x0001, 0, ctypes.pointer(extra))
            x = Input(ctypes.c_ulong(0), ii_)
            ctypes.windll.user32.SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))
            time.sleep(duration / len(coordinates))
def _to_windows_coordinates(x=0, y=0):
    display_width = win32api.GetSystemMetrics(0)
    display_height = win32api.GetSystemMetrics(1)
    windows_x = (x * 65535) // display_width
    windows_y = (y * 65535) // display_height
    return windows_x, windows_y
def _interpolate_mouse_movement(start_windows_coordinates, end_windows_coordinates, steps=20):
    x_coordinates = [start_windows_coordinates[0], end_windows_coordinates[0]]
    y_coordinates = [start_windows_coordinates[1], end_windows_coordinates[1]]
    if x_coordinates[0] == x_coordinates[1]:
        x_coordinates[1] += 1
    if y_coordinates[0] == y_coordinates[1]:
        y_coordinates[1] += 1
    interpolation_func = scipy.interpolate.interp1d(x_coordinates, y_coordinates)
    intermediate_x_coordinates = np.linspace(start_windows_coordinates[0], end_windows_coordinates[0], steps + 1)[1:]
    coordinates = list(map(lambda x: (int(round(x)), int(interpolation_func(x))), intermediate_x_coordinates))