According to this page on MSDN, the Key Value
for Left Control is 162. How can I determine if this key is pressed in code? At the moment, everytime I try and handle a keypress, I get the value 17
which is just a generic control key. Is there a way to differentiate the two? I've tried overriding ProcessCmdKey
and handling PreviewKeyDown
of a Textbox, but they both return 17 instead of 162. I need to do this for all modifier keys and before I end up hardcoding the values, is there a better alternative to capture these in code?
Edit: Code added. To clarify, I want to retrieve the 162 number and not just find an alternative way of differentiating the modifiers.
private void PortfolioNameTextBox_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
var val = e.KeyValue; //17 when control is pressed and not 162
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
//msg.Wparam = 17
return base.ProcessCmdKey(ref msg, keyData);
}
Messr Passant answered this many moons ago.
Would be nice if the KeyEventArgs included it, but nonetheless, you can achieve it like this:
[DllImport("user32.dll")]
private static extern short GetAsyncKeyState(Keys key);
private void textBox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
Console.WriteLine("Ctrl:{0}, LCtrl:{1}, RCtrl:{2}",
GetAsyncKeyState(Keys.ControlKey) < 0,
GetAsyncKeyState(Keys.LControlKey) < 0,
GetAsyncKeyState(Keys.RControlKey) < 0);
}