在游戏应用程序C#HOLD键(C# hold key in a game application)

2019-09-01 18:47发布

我试图做一个C#应用程序,它会控制游戏。 我想要做的是例如:Hold键用于150毫秒,按住左键500毫秒,等等。 我在寻找了很多,我发现下面的代码。 我的程序首先瞄准的游戏,然后按下按键。

I'm holding the keys this way:

Keyboard.HoldKey(Keys.Left);
Thread.sleep(500);
Keyboard.ReleaseKey(Keys.Left);

下面是键盘类:

public class Keyboard
 {
    public Keyboard()
    {
    }

    [StructLayout(LayoutKind.Explicit, Size = 28)]
    public struct Input
    {
        [FieldOffset(0)]
        public uint type;
        [FieldOffset(4)]
        public KeyboardInput ki;
    }

    public struct KeyboardInput
    {
        public ushort wVk;
        public ushort wScan;
        public uint dwFlags;
        public long time;
        public uint dwExtraInfo;
    }

    const int KEYEVENTF_KEYUP = 0x0002;
    const int INPUT_KEYBOARD = 1;

    [DllImport("user32.dll")]
    public static extern int SendInput(uint cInputs, ref Input inputs, int cbSize);

    [DllImport("user32.dll")]
    static extern short GetKeyState(int nVirtKey);

    [DllImport("user32.dll")]
    static extern ushort MapVirtualKey(int wCode, int wMapType);


    public static bool IsKeyDown(Keys key)
    {
        return (GetKeyState((int)key) & -128) == -128;
    }

    public static void HoldKey(Keys vk)
    {
        ushort nScan = MapVirtualKey((ushort)vk, 0);

        Input input = new Input();
        input.type = INPUT_KEYBOARD;
        input.ki.wVk = (ushort)vk;
        input.ki.wScan = nScan;
        input.ki.dwFlags = 0;
        input.ki.time = 0;
        input.ki.dwExtraInfo = 0;
        SendInput(1, ref input, Marshal.SizeOf(input)).ToString();
    }

    public static void ReleaseKey(Keys vk)
    {
        ushort nScan = MapVirtualKey((ushort)vk, 0);

        Input input = new Input();
        input.type = INPUT_KEYBOARD;
        input.ki.wVk = (ushort)vk;
        input.ki.wScan = nScan;
        input.ki.dwFlags = KEYEVENTF_KEYUP;
        input.ki.time = 0;
        input.ki.dwExtraInfo = 0;
        SendInput(1, ref input, Marshal.SizeOf(input));
    }

    public static void PressKey(Keys vk)
    {
        HoldKey(vk);
        ReleaseKey(vk);
    }
}

其在记事本/浏览器等工作,但它不是在任何游戏中工作,无论全屏或窗口模式。 你能帮我找出我怎么能保持在全屏幕应用程序/游戏键? 谢谢!

Answer 1:

“Hold键用于150毫秒,按住左箭头宽度为500ms”

看看这个工程:

        Keyboard.HoldKey((byte)Keys.A, 150);
        Keyboard.HoldKey((byte)Keys.Left, 500);

使用:

public class Keyboard
{
    [DllImport("user32.dll", SetLastError = true)]
    static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

    const int KEY_DOWN_EVENT = 0x0001; //Key down flag
    const int KEY_UP_EVENT = 0x0002; //Key up flag

    public static void HoldKey(byte key, int duration)
    {
        int totalDuration = 0;
        while (totalDuration < duration)
        {
            keybd_event(key, 0, KEY_DOWN_EVENT, 0);
            keybd_event(key, 0, KEY_UP_EVENT, 0);
            System.Threading.Thread.Sleep(PauseBetweenStrokes);
            totalDuration += PauseBetweenStrokes;
        }
    }
}


Answer 2:

我Windown API和SendInput方法做到了。



Answer 3:

你可以得到它与工作,这为按住键的伟大工程:

    public class Keyboard
{

    const int PauseBetweenStrokes = 50;
    [DllImport("user32.dll", SetLastError = true)]
    static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

    const int KEY_DOWN_EVENT = 0x0001; //Key down flag
    const int KEY_UP_EVENT = 0x0002; //Key up flag

    public static void HoldKey(byte key, int duration)
    {
        keybd_event(key, 0, KEY_DOWN_EVENT, 0);
        System.Threading.Thread.Sleep(duration);
        keybd_event(key, 0, KEY_UP_EVENT, 0);
    }
}


文章来源: C# hold key in a game application