C# 在GTA:SA中模拟键盘点击

C# 在GTA:SA中模拟键盘点击,c#,winforms,sendkeys,intptr,C#,Winforms,Sendkeys,Intptr,我试着运行一个程序,有时会模拟GTA:SA中的点击。问题是它在运行时什么都不做。到目前为止,我已尝试过此代码: IntPtr calculatorHandle = FindWindow(null, "GTA:SA:MP"); if (calculatorHandle == IntPtr.Zero) { MessageBox.Show("GTA:SA:MP isn't running"); return; } SetForegroundWindow(calculatorHandle

我试着运行一个程序,有时会模拟GTA:SA中的点击。问题是它在运行时什么都不做。到目前为止,我已尝试过此代码:

IntPtr calculatorHandle = FindWindow(null, "GTA:SA:MP");
if (calculatorHandle == IntPtr.Zero)
{
    MessageBox.Show("GTA:SA:MP isn't running");
    return;
}
SetForegroundWindow(calculatorHandle);
SendKeys.SendWait("T123");
如果你能告诉我我的代码是否有问题,我会很高兴的。谢谢

编辑 这是我添加Neill代码后的代码:

IntPtr calculatorHandle = FindWindow(null, "GTA:SA:MP");
if (calculatorHandle == IntPtr.Zero)
{
    MessageBox.Show("GTA:SA:MP isn't running");
    return;
}
SetForegroundWindow(calculatorHandle);
short key = 0x14;
WindowsMessageService.SendKey(key, KeyFlag.KeyDown);
Thread.Sleep(10);
WindowsMessageService.SendKey(key, KeyFlag.KeyUp);

第一个问题是大多数游戏都使用DirectX输入。你将必须使用以下信息发送游戏密钥。另外,你需要发送上下键来模拟游戏中的按键

[StructLayout(LayoutKind.Sequential)]
struct MouseInput
{
    public int dx;
    public int dy;
    public int mouseData;
    public int dwFlags;
    public int time;
    public IntPtr dwExtraInfo;
}

[StructLayout(LayoutKind.Sequential)]
struct KeyboardInput
{
    public short wVk;      //Virtual KeyCode (not needed here)
    public short wScan;    //Directx Keycode 
    public int dwFlags;    //This tells you what is use (Keyup, Keydown..)
    public int time;
    public IntPtr dwExtraInfo;
}

[StructLayout(LayoutKind.Sequential)]
struct HardwareInput
{
    public int uMsg;
    public short wParamL;
    public short wParamH;
}

[StructLayout(LayoutKind.Explicit)]
struct Input
{
    [FieldOffset(0)]
    public int type;
    [FieldOffset(4)]
    public MouseInput mi;
    [FieldOffset(4)]
    public KeyboardInput ki;
    [FieldOffset(4)]
    public HardwareInput hi;
}

[Flags]
public enum KeyFlag
{       
    KeyDown = 0x0000,
    ExtendedKey = 0x0001,
    KeyUp = 0x0002,
    UniCode = 0x0004,
    ScanCode = 0x0008
}

public static class WindowsMessageService
{
    [DllImport("user32.dll")]
    private static extern UInt32 SendInput(UInt32 nInputs,[MarshalAs(UnmanagedType.LPArray, SizeConst = 1)] Input[] pInputs, Int32 cbSize);

    public static void SendInput(short keycode, KeyFlag keyFlag)
    {
        var inputData = new Input[1];

        inputData[0].type = 1;
        inputData[0].ki.wScan = keycode;
        inputData[0].ki.dwFlags = (int)keyFlag;
        inputData[0].ki.time = 0;
        inputData[0].ki.dwExtraInfo = IntPtr.Zero;

        SendInput(1, inputData, Marshal.SizeOf(typeof(Input)));
    }

    public static void SendKey(short keyCode, KeyFlag keyFlag)
    {
        SendInput(keyCode, keyFlag | KeyFlag.ScanCode);
    }
}
用法示例:

short key = 0x3B; // F1 key

WindowsMessageService.SendKey(key, KeyFlag.KeyDown);

Thread.Sleep(10);

WindowsMessageService.SendKey(key, KeyFlag.KeyUp);

我只是用
short key=0x14尝试了一下(T键)但它也不起作用。你确定你有指向GTA进程主窗口的指针吗?我所做的是自己使用ProcessStartInfo和process.Start启动游戏。流程实例提供MainWindow属性,在发送密钥之前,必须使用该属性设置ForeGroundWindow。或者您可以使用:Process.getProcessByName(processName).FirstOrDefault()附加到游戏的运行进程。我用新代码编辑了我的帖子,有什么问题吗?没关系,我已经解决了。它不起作用,因为我以管理员身份运行GTA,所以我也必须以管理员身份运行该程序。现在我的问题是,是否可以从字符串发送一个键序列。如何将字符转换为代码?