2013-05-28 112 views
6

我知道如何处理关键事件,即处理VirtualKey在Windows 8商店应用程序用C#

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”和大写'之间进行辨别什么一个'?另外,如果我想处理百分号'%'之类的键,该怎么办?

回答

1

由于KeyUp只知道哪些键被按下,而不是输入哪些字母,所以不能轻易从KeyUp获取此信息。你可以检查shift键是否关闭,你也可以尝试跟踪大写锁定自己。更好地使用TextChanged事件。

+0

谢谢Xyroid。我试图处理一种情况,即一次一个地评估击键,并且基于按下的第一个键来调用代码。不幸的是,TextChanged将无法工作,因为击键不会一次全部进入。 – joelc

+0

当有人试图使用日文键盘时,你会感到非常惊讶。将键转换为字符非常困难。让输入管理器处理它。 –

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

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

+2

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

+1

很高兴它帮助你出来! – joelc

+1

谢谢!它也帮助我回答我自己的问题:) http://stackoverflow.com/questions/25475739/activate-a-textbox-automatically-when-user-starts-typing –

相关问题