Detect if Modifier Key is Pressed in KeyRoutedEventArgs Event

后端 未结 4 807
梦毁少年i
梦毁少年i 2020-12-06 10:55

I have the following code:

public void tbSpeed_KeyDown(object sender, KeyRoutedEventArgs e)
{
    e.Handled = !((e.Key >= 48 && e.Key <= 57) ||         


        
4条回答
  •  不思量自难忘°
    2020-12-06 11:14

    Bitwise AND the Modifiers property of Keyboard with Shift Key -

    bool isShiftKeyPressed = (Keyboard.Modifiers & ModifierKeys.Shift)
                             == ModifierKeys.Shift;
    

    Try this too-

    bool isShiftKeyPressed = (ModifierKeys & Keys.Shift) == Keys.Shift;
    

    OR

    Control.ModifierKeys == Keys.Shift
    

提交回复
热议问题