I’m making an application in C# With Visual Studio 2010. It is an Windows Forms Application
What I want to do is to trigger an function when 3 keys are pressed. The keys are CTRL, SHIFT and X.
I have this code right now:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
Console.WriteLine(e.KeyValue);
/*
*KeyValue:
*CTRL = 17
*Shift = 16
*X = 88
*/
}
And as you can see, its pretty empty. This is because I don’t know it anymore.
I’ve found on the internet this piece of code, but don’t know how to implement it:
[DllImport("user32.dll")]
public static extern int GetKeyboardState(byte[] keystate);
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
byte[] keys = new byte[255];
GetKeyboardState(keys);
if (keys[(int)Keys.Up] == 129 && keys[(int)Keys.Right] == 129)
{
Console.WriteLine("Up Arrow key and Right Arrow key down.");
}
}
So my question to you guys is, how can i check if all the tree keys are pressed at the same time?
Or if you know an better way to make such an hot key, how would that be than?
In the case of CTRL+SHIFT+X, only X is really a proper key; the others are “modifier” keys. So no need to break out the P/Invoke 🙂 Just do this in your Key_Down handler: