Get lowercase with keydown wpf

前端 未结 5 2065
再見小時候
再見小時候 2020-12-20 17:36

I want to get the keys pressed on the keyboard either with or without caplock:

private void Window_KeyDown(object sender, System.Windows.Input.KeyEventArgs e         


        
5条回答
  •  失恋的感觉
    2020-12-20 18:27

    You can check in your KeyDown event whether CAPS lock is on or off using Control.IsKeyLocked method. Also, you might need to check if user typed in capital using Shift key which can be identified using Modifiers enum like this -

    private void Window_KeyDown(object sender, KeyEventArgs e)
    {
       string key = e.Key.ToString();
       bool isCapsLockOn = System.Windows.Forms.Control
                            .IsKeyLocked(System.Windows.Forms.Keys.CapsLock);
       bool isShiftKeyPressed = (Keyboard.Modifiers & ModifierKeys.Shift)
                                    == ModifierKeys.Shift;
       if (!isCapsLockOn && !isShiftKeyPressed)
       {
          key = key.ToLower();
       }
    }
    

提交回复
热议问题