我为directx游戏搜索了大量的模拟鼠标点击和移动。我找到了一个关于按键的很好的消息来源,但对鼠标没有任何帮助。实际上,关于带直接输入的按键(模拟Python按键来控制游戏),有一个很好的堆栈溢出主题。但我没有足够的经验,使它的工作鼠标点击在特定的位置。
我尝试了很多python模块,比如pyautogui、win32等等--它们不起作用。甚至我也试图通过自动热键将参数发送到“.ahk”文件,但这并不稳定,也不是一个好方法。我将感谢您的每一个评论。我只做了两天的点击,我完全迷路了。谢谢!
在游戏中,我从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()函数正在工作,但是单击“对所有模块都有效”,我需要的是set_pos()来工作,但不幸的是,它不能工作。
发布于 2019-03-03 17:09:15
我做了很多研究,发现了吡咯烷酮模块。它真的很棒,而且很容易使用。我认为它只适用于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)发布于 2019-03-02 08:15:29
对于Directx游戏,如果Directx游戏确实有效,您必须自行测试它。
但是使用pywinauto,您可以模拟鼠标点击和鼠标移动。
要安装python 27的pywinauto,可以使用这个bat文件。
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按钮图像发送任何键盘快捷键,宏的或MouseClicks或应用程序或游戏。(在不松开焦点的情况下,活动Windows.)
发布于 2020-04-05 21:37:08
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
https://stackoverflow.com/questions/54954041
复制相似问题