2013-01-23 238 views
17

我想用我的Kinect和Python来控制游戏(我的两个测试游戏是Half Life 2和Minecraft)。除了一件事以外,一切都有效。游戏将响应模拟的鼠标事件和模拟的鼠标移动(鼠标事件通过ctypes完成,鼠标移动使用pywin32完成)。但问题在于游戏忽略了模拟按键。他们两个都会在聊天窗口(Minecraft)或开发者控制台(Half Life 2)中选择模拟按键,但不会在玩实际游戏时使用。模拟Python按键控制游戏

我试着发送按键的几种方法:

import win32com.client as client 
wsh = client.Dispatch('WScript.Shell') 
wsh.AppActivate(gameName) 
wsh.SendKeys(key) 

和:

import win32api 
win32api.keybd_event(keyHexCode, 0, 0) 

和:

import ctypes 
import time 

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

我应该在最后一个点出码不是我的,这里关于Stack Overflow的另一个问题。

有谁知道为什么这些工作都没有,以及正确的方法是什么?

+1

代替适当的反应,可能我建议检查的Minecraft的来源,看是否存在键盘事件是如何收集和处理的区别在聊天和游戏之间?当我回家时,我会自己看看它。 – Logan

+0

我将不得不撤回我早期的声明,其实它并不适用于Minecraft,它使用ctypes系统。我现在仔细检查半条命2 ... – user573949

+0

是的,它仍然没有工作。 – user573949

回答

3

这很可能是游戏使用DirectInput设备。

所以,游戏期待DirectInput按键。根据this forum thread的最后一篇文章,DirectInput响应ScanCodes,而不是VK。您可以尝试使用this tool发送DirectInput按键。开发者还提供了源和详细的解释。

如果这样做,你可以尝试发送适当的扫描码而不是VK (list of scancodes)

还有一个较旧的项目叫做DirectPython,它允许你与DirectX/DirectInput进行接口。

+0

该工具的下载链接是死了,你知道如何用DirectPython实际发送DirectInput按键吗?它似乎只是为了倾听他们,而不是派遣他们。 – user573949

19

我刚刚尝试模拟Half-Life 2中的按键时遇到了同样的问题。正如Robin所说,解决方案是使用ScanCodes代替VK。

我编辑了您的上一个代码示例,以便它使用ScanCodes。我半条命2尝试过了,它工作得很好:

import ctypes 
import time 

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

# directx scan codes http://www.gamespp.com/directx/directInputKeyboardScanCodes.html 
while (True): 
    PressKey(0x11) 
    time.sleep(1) 
    ReleaseKey(0x11) 
    time.sleep(1)