I want to determine whether a CTRL key is LEFT CTRL or RIGHT CTRL key when it is pressed. How can I do this?
Apparently not from within .NET, but it's possible from the Win32 APIs.
You can easily check the status of the Keyboard using System.Windows.Input.Keybaord.IsKeyDown()
to determine if the Right
or Left
Control
key is pressed:
if (Keyboard.IsKeyDown(Key.LeftCtrl)
{}
else if (Keyboard.IsKeyDown(Key.RightCtrl)
{}
end if
AFAIK you can't access this information from within .net.
However, you can use the Win32 API GetAsyncKeyState
to test if specific keys are currently down, and this can differentiate the left and right ctrl keys. (If you're writing a game this is more likely to work well for you than Keydown handlers, as GetAsyncKeyState tests whether the key is down "now" rather than whether it was pressed "at some time in the past", which gives considerably better responsiveness).
Since you're writing a game, it might be helpful to also use DirectInput... Winforms isn't really meant for da gaimez IMO...
Device di_device = new Device(SystemGUID.Keyboard);
di_device.SetCooperativeMode(Nonexclusive|othercrap);
di_device.Acquire();
if(di_device.GetKeyboardState()[Keys.LControl])
{
blargh;
}
Code written in SO textbox, untested, something like that. Note that you must include Microsoft.DirectX.DirectInput
Also note that i don't mean you should init a DirectInput device every time you need to get input. Create the device when you init your game, dispose of it when your program exits.
DirectInput gives you more control... And chances are, you're going to want to use it. Of course at certain points, you will need to use Winforms instead [ie: while your program is rendering, what if the user presses a button pretty quickly? GKS won't tell you about it]