【问题标题】:Use Python to send keystrokes to games in Windows?使用 Python 向 Windows 中的游戏发送击键?
【发布时间】:2014-05-26 07:31:25
【问题描述】:

我一直在 Windows 环境中使用 Python,并且我编写了一个脚本来自动化已知游戏中的一些任务。 该任务涉及大量使用鼠标和键盘输入。

然而,所述脚本只有一个问题:它无法将击键发送到应用程序。我已经尝试了至少 3 种不同的方法,我将在下面发布这些方法和一些变体(还阅读了十分之一的类似问题/答案,但无济于事)

第一个,使用win32api 模块:

f = 0x46 # VirtualKey Code of the letter "F", see http://msdn.microsoft.com/en-us/library/windows/desktop/dd375731%28v=vs.85%29.aspx 

win32api.keybd_event(f,0,0,0) # holds the "F" key down
time.sleep(2) # waits 2 seconds
win32api.keybd_event(f,0,win32con.KEYEVENTF_KEYUP,0) # releases the key

没有什么特别之处,在任何文本编辑器、浏览器中都能完美运行(输入“f”)...但是,如果我打开诸如反恐精英之类的游戏,那么击键就会“丢失”-就像,什么都没有发生。另一方面,如果我打开 Counter-Strike 的控制台,那么击键 get 会被注册(就像在记事本中一样)。 在另一款游戏《英雄联盟》中进行了测试,行为完全相同。在实际游戏中,没有检测到击键。但是,如果我打开聊天(仍然是游戏内)并重新运行脚本,那么它就会被聊天注册。

进入第二种方法:

shell = win32com.client.Dispatch("WScript.Shell")
shell.SendKeys("F")

与上面的行为完全相同。除了游戏之外的所有东西都可以正常工作,而且只能在聊天中使用。

第三种方法(归功于在另一个 stackoverflow 线程中发布它的人),使用ctypes 模块更高级(调用SendInput())。理论上,在这三个中,这个最接近模拟实际的物理按键:

SendInput = ctypes.windll.user32.SendInput

# 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 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( hexKeyCode, 0x48, 0, 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( hexKeyCode, 0x48, 0x0002, 0, ctypes.pointer(extra) )
    x = Input( ctypes.c_ulong(1), ii_ )
    ctypes.windll.user32.SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))


def KeyPress():
    PressKey(0x46) # press F
    time.sleep(.5)
    ReleaseKey(0x46) #release F

...它也不起作用。奇怪的是,它显示了与前三个完全相同相同的行为:在任何文本编辑器/简单应用程序中工作、被游戏忽略或仅在游戏聊天部分中注册。

如果我猜的话,我会说这些游戏以其他方式获取键盘事件,而这三种方法中的任何一种都没有涉及,因此忽略了这些方法。

如果有任何帮助,我将不胜感激。如果可能,请提供在 CS、LoL 或类似游戏中工作的具体代码示例,以便我有一个起点。

【问题讨论】:

  • 对于基于浏览器的游戏,我发现您上面描述的基本按键在 Firefox 浏览器上有效,但不适用于 chrome。

标签: python winapi ctypes keystrokes


【解决方案1】:

您可以尝试使用EnumWindows() 枚举应用程序的窗口,并直接将SendMessage() 调用到游戏的主窗口。

【讨论】:

    【解决方案2】:

    您的游戏可能使用 DirectInput。使用其中任何一种方法尝试 DirectInput 扫描码。快速搜索一下这个网站:http://www.gamespp.com/directx/directInputKeyboardScanCodes.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-19
      • 1970-01-01
      相关资源
      最近更新 更多