Detect Shift+1 as Key.Add
I wou开发者_如何转开发ld like to detect the user pressing the "add" key in the .net 4 WPF KeyDown event handler. To do this I use the following test:
if (e.Key == Key.Add)
This doesn't detect the case when the user presses Shift+1 (which corresponds to "add" on my keyboard layout).
How can I detect this? I'm not convinced that testingif (e.Key == Key.D1 && Keyboard.Modifiers == ModifierKeys.Shift)
is the right solution as it may be mapped elsewhere on another keyboard layout.
Any suggestions?
You might consider using the KeyPress
event handler instead.
private void trackBarFrames_KeyDown(object sender, KeyEventArgs e)
{
switch ( e.KeyCode)
{
case Keys.Add :
// Nummeric Keypad Add
AddSomething();
break;
case Keys.Oemplus :
// Regular keyboard Add
// OemPlus is assigned to the regular keyboard key with a "Add" Sign but doesn not take shift conditions in account..!
if (e.Modifiers == Keys.Shift)
{
AddSomething();
}
break;
}
}
精彩评论