使用 keydown wpf 获取小写 [英] Get lowercase with keydown wpf

查看:28
本文介绍了使用 keydown wpf 获取小写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在有或没有 caplock 的情况下按下键盘上的键:

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)
{
   e.Key.ToString();
}

当我在键盘上输入a"或A"时,e.Key的结果总是A".输入a"如何获得a"?

When I type "a" or "A" on the keyboard, the result of e.Key is always "A". How can I get 'a' for entering 'a'?

推荐答案

您可以使用 KeyDown 事件中检查 CAPS lock 是打开还是关闭"http://msdn.microsoft.com/en-us/library/system.windows.forms.control.iskeylocked%28VS.80%29.aspx" rel="noreferrer" title="Control.IsKeyLocked">控制.IsKeyLocked 方法.此外,您可能需要检查用户是否使用 Shift 键输入了大写字母,该键可以使用 Modifiers 枚举来识别 -

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();
   }
}

这篇关于使用 keydown wpf 获取小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆