游戏中的Python鼠标移动仿真 [英] Python Mouse Movement Emulation in Games

查看:833
本文介绍了游戏中的Python鼠标移动仿真的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究使用Kinect玩视频游戏.这需要模拟键盘和鼠标事件.我已经弄清楚了带有ctypes的SendInput()的键盘事件在Skyrim和Minecraft中都可以使用.我还发现ctypes的mouse_event()在Skyrim和Minecraft中均可用于模拟鼠标按钮.这些是我到目前为止发现的解决方案,似乎对Skyrim和Minecraft都适用.

I'm looking into using the Kinect to play video games. This requires the emulation of both keyboard and mouse events. I've figured out that keyboard events with ctypes' SendInput() works in both Skyrim and Minecraft. I've also found that ctypes' mouse_event() works in both Skyrim and Minecraft for emulating mouse buttons. Those are the solutions I've found so far that seem to work for both Skyrim and Minecraft.

我遇到的主要问题是在Skyrim内移动播放器的摄像头.我可以使用ctypes的SetUserPos()和ctypes的mouse_event()在Minecraft中移动相机.但是,这两种解决方案都不适用于Skyrim.我也尝试过使用SendInput()在Skyrim中移动播放器的摄像头,但是每次使用SendInput()(即使在Skyrim之外)移动光标时,我的监视器也会暂时变黑,并且摄像头仍然不会移动(我不知道为什么会发生类似的事情...)

The main issue I'm having is with moving the player's camera around within Skyrim. I'm able to move the camera around in Minecraft using ctypes' SetUserPos() and also ctypes' mouse_event(). But neither solutions work for Skyrim. I've also tried using SendInput() to move the player's camera in Skyrim, but every time the cursor gets moved with SendInput() (even outside of Skyrim) my monitor goes blank for a moment and the camera still doesn't move (I have no idea why something like that is happening...)

因此,无论如何,我是否有可能能够模拟Skyrim或其他可能以类似方式处理其鼠标输入的游戏中的相机运动?我愿意使用C/C ++来完成此任务,但是更喜欢直接使用Python.感谢您提出的所有建议!

So is there anyway that I could possibly be able to emulate the camera movement within Skyrim or other games that probably handle their mouse inputs similarly? I'm willing to use C/C++ for this task, but straight Python would be more preferable. Thanks for any and all suggestions you may have!

我还将保留用于使显示器空白的代码.我以为SendInput()可能不适用于Skyrim中的相机移动,但也许我只是做了一些非常错误的事情. (我还从众多线程中获取了以下大多数代码.)

I'm also going to leave the code I used to make my monitor go blank. I'm thinking SendInput() probably won't work for camera movement within Skyrim, but maybe I just did something terribly wrong. (I also got most of the below code from a multitude of threads online.)

import ctypes
import time

# C struct redefinitions
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 POINT(ctypes.Structure):
        _fields_ = [("x", ctypes.c_ulong),
                    ("y", ctypes.c_ulong)]


class Input_I(ctypes.Union):
    _fields_ = [("ki", KeyBdInput),
                 ("mi", MouseInput),
                 ("hi", HardwareInput)]


class Input(ctypes.Structure):
    _fields_ = [("type", ctypes.c_ulong),
                ("ii", Input_I)]

# Actuals Functions


def PressKey(hexKeyCode):
    extra = ctypes.c_ulong(0)
    ii_ = Input_I()
    ii_.ki = KeyBdInput(0, hexKeyCode, 0x0008, 0, ctypes.pointer(extra))
    x = Input(ctypes.c_ulong(1), ii_)
    ctypes.windll.user32.SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))


def ReleaseKey(hexKeyCode):
    extra = ctypes.c_ulong(0)
    ii_ = Input_I()
    ii_.ki = KeyBdInput(0, hexKeyCode, 0x0008 | 0x0002, 0, ctypes.pointer(extra))  # lint:ok
    x = Input(ctypes.c_ulong(1), ii_)
    ctypes.windll.user32.SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))

def MoveMouse(x, y):
    extra = ctypes.c_ulong(0)
    ii_ = Input_I()
    x = int(x*(65536/ctypes.windll.user32.GetSystemMetrics(0))+1)
    y = int(y*(65536/ctypes.windll.user32.GetSystemMetrics(1))+1)
    ii_.mi = MouseInput(x, y, 0, 0x0001 | 0x8000, 1, ctypes.pointer(extra))
    x = Input(ctypes.c_ulong(0), ii_)
    ctypes.windll.user32.SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))

def main():
    mouse = Mouse()
    time.sleep(3)
    MoveMouse(100, 100)

main()

推荐答案

您可以从PyPI安装PyAutoGUI GUI自动化模块(运行pip install pyautogui),然后调用pyautogui.moveTo()单击特定的X和Y坐标.屏幕:

You can install the PyAutoGUI GUI automation module from PyPI (run pip install pyautogui) and then call the pyautogui.moveTo() to click on a certain X and Y coordinates of the screen:

>>> import pyautogui
>>> pyautogui.moveTo(50, 100)

在Windows的幕后,它会生成与您正在使用的类似的ctypes代码,因此在天际"之类的游戏中它可能可以控制摄像机.

Behind the scenes on Windows, it makes similar ctypes code you are using, so it might work to control the camera in a game like Skyrim.

PyAutoGUI可在Windows,Mac和Linux以及Python 2和3上运行.它还可以模拟键盘,拖动鼠标,截取屏幕截图以及对屏幕截图进行简单的图像识别.完整文档位于 https://pyautogui.readthedocs.org/

PyAutoGUI works on Windows, Mac, and Linux, and on Python 2 and 3. It also can emulate the keyboard, do mouse drags, take screenshots, and do simple image recognition of the screenshots. Full docs are at https://pyautogui.readthedocs.org/

这篇关于游戏中的Python鼠标移动仿真的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆