在Windows 8中处理VirtualKey使用C#存储应用程序

joe*_*elc 6 windows-8 windows-store-apps

我知道如何处理关键事件,即

private void Page_KeyUp(object sender, KeyRoutedEventArgs e)
{
  switch (e.Key)
  {
    case Windows.System.VirtualKey.Enter:
      // handler for enter key
      break;

    case Windows.System.VirtualKey.A:
      // handler for A key
      break;

    default:
      break;
  }
}
Run Code Online (Sandbox Code Playgroud)

但是如果我需要在小写'a'和大写'A'之间辨别怎么办?另外,如果我想处理百分号'%'之类的键,该怎么办?

joe*_*elc 8

其他地方有答案.对于那些感兴趣的人......

public Foo()
{
    this.InitializeComponent();
    Window.Current.CoreWindow.CharacterReceived += KeyPress;
}

void KeyPress(CoreWindow sender, CharacterReceivedEventArgs args)
{
    args.Handled = true;
    Debug.WriteLine("KeyPress " + Convert.ToChar(args.KeyCode));
    return;
}
Run Code Online (Sandbox Code Playgroud)

更好的是,将其移动Window.Current.CoreWindow.CharacterReceived += KeyPress;到GotFocus事件处理程序中,并添加Window.Current.CoreWindow.CharacterReceived -= KeyPress;到LostFocus事件处理程序中.

  • 男人,你的答案是唯一的,我的意思是只能帮助我在网上找到有关CharacterReceived事件的文档.所以它引导我在这里回答我自己的问题:http://stackoverflow.com/questions/24612653/windows-phone-8-1-shift-key-state-abnormal-behaviour非常感谢你 (2认同)

Xyr*_*oid 1

您无法轻松获取此信息,KeyUp因为KeyUp只知道按下了哪些键,而不知道正在键入哪些字母。您可以检查是否按下了 Shift 键,也可以尝试自己跟踪大写锁定。最好使用TextChanged事件。