首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Python鼠标点击游戏(直接输入)

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

Stack Overflow用户
提问于 2019-03-02 00:27:09
回答 3查看 21.5K关注 0票数 2

我为directx游戏搜索了大量的模拟鼠标点击和移动。我找到了一个关于按键的很好的消息来源,但对鼠标没有任何帮助。实际上,关于带直接输入的按键(模拟Python按键来控制游戏),有一个很好的堆栈溢出主题。但我没有足够的经验,使它的工作鼠标点击在特定的位置。

我尝试了很多python模块,比如pyautogui、win32等等--它们不起作用。甚至我也试图通过自动热键将参数发送到“.ahk”文件,但这并不稳定,也不是一个好方法。我将感谢您的每一个评论。我只做了两天的点击,我完全迷路了。谢谢!

在游戏中,我从reddit中找到了这个代码,但是它也不起作用。

代码语言:javascript
复制
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()函数正在工作,但是单击“对所有模块都有效”,我需要的是set_pos()来工作,但不幸的是,它不能工作。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-03-03 17:09:15

我做了很多研究,发现了吡咯烷酮模块。它真的很棒,而且很容易使用。我认为它只适用于32位python,我无法将其安装到64位版本。您也应该安装autoitx3,但我不确定它是否可以只使用一个dll。安装autoitx3和pyautoit模块后,可以使用以下示例代码:

代码语言:javascript
复制
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)
票数 4
EN

Stack Overflow用户

发布于 2019-03-02 08:15:29

对于Directx游戏,如果Directx游戏确实有效,您必须自行测试它。

但是使用pywinauto,您可以模拟鼠标点击和鼠标移动。

看看pywinauto.mouse

要安装python 27的pywinauto,可以使用这个bat文件。

install-Pywinauto.bat

代码语言:javascript
复制
C:\Python27\scripts\pip.exe install pywinauto
pause

现在你准备好了。

Left-mouse-click.py

代码语言:javascript
复制
import pywinauto
pywinauto.mouse.click(button='left', coords=(0, 0)) 

Double-Left-Mouse-Click.py

代码语言:javascript
复制
import pywinauto
pywinauto.mouse.double_click(button='left', coords=(0, 0))

如果你把它和AutoPytonLauncher一起使用

您可以单击桌面上的3d按钮图像发送任何键盘快捷键,宏的MouseClicks应用程序或游戏。(在不松开焦点的情况下,活动Windows.)

票数 1
EN

Stack Overflow用户

发布于 2020-04-05 21:37:08

代码语言:javascript
复制
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))

将此代码附加到您的代码中,并使用move(x,y)调用它。这是指向完整代码controller.py的链接,它还可能有助于以管理权限运行python

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54954041

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档