Windows 8 使用C处理Windows 8应用商店应用程序中的VirtualKey#

Windows 8 使用C处理Windows 8应用商店应用程序中的VirtualKey#,windows-8,windows-store-apps,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

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

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

但如果我需要区分小写字母“a”和大写字母“a”,该怎么办?另外,如果我想处理百分号“%”之类的键,该怎么办?

您无法从
KeyUp
轻松获取此信息,因为
KeyUp
只知道按下了哪些键,而不知道键入了哪些字母。您可以检查换档钥匙是否已按下,也可以尝试跟踪caps lock(自动锁定)。最好使用
TextChanged
事件。

在别处找到答案。对于那些感兴趣的人

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

更好的是,移动
Window.Current.CoreWindow.CharacterReceived+=按键编码到GotFocus事件处理程序中,并添加
Window.Current.CoreWindow.CharacterReceived-=KeyPress编码到LostFocus事件处理程序中。

谢谢Xyroid。我试图处理这样一种情况:每次对击键求值一次,然后根据按下的第一个键调用代码。不幸的是,TextChanged不起作用,因为按键不会一次全部输入。当有人试图使用日文键盘时,你会大吃一惊。将键转换为字符非常困难。让输入管理器来处理。伙计,你的回答是我在网上找到的唯一关于CharacterReceived事件的帮助文档。因此,它引导我在这里回答了我自己的问题:非常感谢你,很高兴它帮助了你!!非常感谢。这也帮助我回答了我自己的问题:)这只是另一个谢谢。我花了大量的时间寻找正确的方法来做这件事!