简体   繁体   English

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

[英]Python Mouse Click For Game (Direct Input)

I searched a lot for simulate mouse clicks and movement for directx games.我搜索了很多模拟鼠标点击和 Directx 游戏的移动。 I found a good sources about keypressing but nothing for mouse.我找到了一个关于按键的好资料,但没有找到关于鼠标的资料。 Actually there is good stackoverflow topic about key press with direct input ( Simulate Python keypresses for controlling a game ).实际上有关于直接输入按键的很好的计算器主题( 模拟 Python 按键控制游戏)。 But i don't have enough experience to make it work for mouse clicks on certain location.但是我没有足够的经验来使其适用于特定位置的鼠标点击。

I tried a lot of python module like pyautogui, win32 etc. they are not working.我尝试了很多 python 模块,如 pyautogui、win32 等。它们都不起作用。 Even i tried to click it over autohotkey with sending arguments to '.ahk' file but it's not stable and not a good way.甚至我尝试通过自动热键单击它并将参数发送到“.ahk”文件,但它不稳定,也不是一个好方法。 I will appreciate for every comment.我会感谢每一条评论。 I'm working on just this click for 2 days and i'm totally lost.我只在这个点击上工作了 2 天,我完全迷失了。 Thanks!谢谢!


I found this code from reddit for clicks in game but it's not working either.我从 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() function is working but click works with all modules what i need is set_pos() to work but unfortunately it's not. left_click() 函数正在工作,但 click 可以与所有模块一起使用,我需要 set_pos() 才能工作,但不幸的是它不是。

I did a lot of research and found pyautoit module.我做了很多研究,发现了 pyautoit 模块。 It's really great and easy to use.它真的很棒而且易于使用。 I think it's only works with 32 bit python i couldn't install it to 64 bit version.我认为它只适用于 32 位 python 我无法将它安装到 64 位版本。 You should install autoitx3 too but i'm not sure maybe it can work with just one dll.您也应该安装 autoitx3,但我不确定它是否只能与一个 dll 一起使用。 After install autoitx3 and pyautoit module you can use this sample code :安装 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)

For Directx Games you must Self test it out, if the Direct Input does Works.对于 Directx 游戏,如果 Direct Input 有效,您必须对其进行自我测试。

But with pywinauto Package you can simulate Mouse Clicks and Mouse Movements.但是使用 pywinauto 包,您可以模拟鼠标点击和鼠标移动。

Look at pywinauto.mouse看看pywinauto.mouse

To Install pywinauto Package for python 27 you can use this bat file.要为 python 27 安装 pywinauto 包,您可以使用此 bat 文件。

install-Pywinauto.bat安装-Pywinauto.bat

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

Now you are ready.现在你准备好了。

Left-mouse-click.py鼠标左键单击.py

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

Double-Left-Mouse-Click.py鼠标左键双击.py

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

and if you use it together with AutoPytonLauncher如果您将它与AutoPytonLauncher一起使用

you can Click a 3d Button Image on your Desktop to send any Keyboard Shortcuts Macro's or MouseClicks or MouseMovements to Windows Applications or Games.你可以点击桌面上的一个3D按钮图像发送任何快捷键的宏的鼠标点击或或MouseMovements到Windows应用程序或游戏。 (without Loosing Focus the Active Windows.) (不会失去对活动窗口的关注。)

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))

Attach this code to yours and call it using move(x,y) .将此代码附加到您的代码并使用move(x,y)调用它。 This is the link to the full code https://github.com/SerpentAI/SerpentAI/blob/dev/serpent/input_controllers/native_win32_input_controller.py Also it might help to run python with Administrative privileges这是完整代码的链接https://github.com/SerpentAI/SerpentAI/blob/dev/serpent/input_controllers/native_win32_input_controller.py也可能有助于以管理权限运行 python

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

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