本文介绍了使用 C# 在 Windows 8 Store 应用程序中处理 VirtualKey的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm aware of how to handle key events, i.e.

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"怎么办?另外,如果我想处理像百分号%"这样的键怎么办?

But what if I need to discern between lower-case 'a' and upper-case 'A'? Also, what if I want to handle keys like the percent sign '%'?

推荐答案

在别处得到了答案.对于那些有兴趣的人...

Got an answer elsewhere. For those that are interested...

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 += KeyPress; 移动到 GotFocus 事件处理程序中,并添加 Window.Current.CoreWindow.CharacterReceived -= KeyPress;code> 到 LostFocus 事件处理程序中.

Even better, move the Window.Current.CoreWindow.CharacterReceived += KeyPress; into a GotFocus event handler, and add Window.Current.CoreWindow.CharacterReceived -= KeyPress; into a LostFocus event handler.

这篇关于使用 C# 在 Windows 8 Store 应用程序中处理 VirtualKey的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 01:11