2013-05-28 159 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 –

相關問題