2017-07-17 55 views
1

我正在創建一個UWP應用程序(C#.NET),其中有文本框。我想實現一個快捷方式(Ctrl + F)來搜索文本框中的文本。我知道如何找到文本,但我不知道如何實現快捷方式。UWP Ctrl + F實現

我發現這一點:

if ((e.Control && e.KeyCode == Keys.F) || (e.Control && e.KeyCode == Keys.S)) 
{ 
    //do something 
} 

...但它不工作UWP。我試過這個(textarea是文本框的名稱):

private void textarea_KeyDown(object sender, KeyRoutedEventArgs e) 
{ 
    if ((e.Key == Windows.System.VirtualKey.Control) && (e.Key == Windows.System.VirtualKey.F)) 
    { 
     flayoutFind.ShowAt(appBarButtonFind as FrameworkElement); 
    } 
} 

但它不工作。我該怎麼做?

和未來,有沒有什麼辦法,如何覆蓋默認功能和文本框按Ctrl +ž快捷鍵(撤銷)?

+0

根據[這個答案](https://stackoverflow.com/a/32782168/6741868)你可以嘗試使用'Window.Current.CoreWindow.GetKeyState(VirtualKey.Control)'。 –

回答

4

您應該使用這裏描述的「加速器」和「訪問鍵」: https://docs.microsoft.com/en-us/windows/uwp/input-and-devices/keyboard-interactions

基本上,你將不得不事件

Window.Current.CoreWindow.Dispatcher.AcceleratorKeyActivated += Dispatcher_AcceleratorKeyActivated; 

private void Dispatcher_AcceleratorKeyActivated(CoreDispatcher sender, AcceleratorKeyEventArgs args) 
    { 
     //Implementation 
    } 

您可以在這裏詳細檢查樣品登記:https://github.com/Microsoft/DesktopBridgeToUWP-Samples/blob/master/Samples/SQLServer/BuildDemo/MainPage.xaml.cs