C# - SendInput() - Hold Key Down?

1.2k views Asked by At

Current Functionality
In a Direct3D environment, the key is held down. In a non Direct3D environment, it is simply pressed once.

    public static void HoldKey()
    {
        //Type = 1 means keyboard input.
        INPUT IP = new INPUT { Type = 1 };
        IP.Data.Keyboard = new KEYBDINPUT();
        IP.Data.Keyboard.VirtualCode = 0;  //Zero for ignore virtual code.
        IP.Data.Keyboard.ScanCode = 0x39; // Spacebar scancode.
        IP.Data.Keyboard.Flags = 0x0008; //KEYEVENTF_SCANCODE
        IP.Data.Keyboard.Time = 0;
        IP.Data.Keyboard.ExtraInfo = IntPtr.Zero;

        INPUT[] Inputs = new INPUT[] { IP };
        //Key Press.
        SendInput(1, Inputs, Marshal.SizeOf(typeof(INPUT)));
    }

It should be noted, that I am used the ScanCode field instead of the VirtualCode field because I do wish to send inputs to Direct3D applications.

Desired Functionality
I would like for the key to be held down, in both a Direct3D environment and a non Direct3D environment, akin to my holding down of the corresponding button on my physical keyboard.

Additional Information
I have seen some code using the keybd_event() function, although it does say on the MSDN website that it has been 'superseded'.

I have considered using the SendMessage() function instead to send a WM_KEYDOWN message, but I'm not sure if this would work.

0

There are 0 answers