2010-01-25 85 views
11

在我的應用程序中,我想讓用戶自定義鍵盤快捷鍵,就像在Visual Studio的鍵盤選項中完成一樣。用戶可以關注一個空白文本框,然後鍵入他想要分配給命令的任何快捷方式。如何從WPF中的用戶讀取自定義鍵盤快捷鍵?

我來使其工作最接近的是通過訂閱TextBox.PreviewKeyDown事件,將其設置爲處理,以防止在文本框中實際的文字輸入。然後,我忽略與修飾鍵相關的KeyDown事件(是否有更簡潔的方式來確定鍵是修飾鍵?)。

// Code-behind 
private void ShortcutTextBox_PreviewKeyDown(object sender, KeyEventArgs e) 
{ 
    // The text box grabs all input 
    e.Handled = true; 

    if (e.Key == Key.LeftCtrl || 
     e.Key == Key.RightCtrl || 
     e.Key == Key.LeftAlt || 
     e.Key == Key.RightAlt || 
     e.Key == Key.LeftShift || 
     e.Key == Key.RightShift) 
     return; 

    string shortcutText = ""; 
    if ((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control) 
     shortcutText += "Ctrl+"; 
    if ((Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift) 
     shortcutText += "Shift+"; 
    if ((Keyboard.Modifiers & ModifierKeys.Alt) == ModifierKeys.Alt) 
     shortcutText += "Alt+"; 
    _ShortcutTextBox.Text = shortcutText + e.Key.ToString(); 

} 

上述作品開始用Ctrl和按Ctrl + Shift任何快捷鍵,但沒有任何Alt鍵的快捷方式。當我按下包含Alt的快捷鍵時,e.Key始終設置爲Key.System

如何從用戶記錄Alt快捷鍵?是否有更好,更強大的方式來記錄用戶的快捷方式?

+0

我感覺Alt鍵被用於通過WPF嘗試捕獲Alt鍵組合集中在其上被綁定到一個標籤與助記符快捷方式(如Alt + F鍵爲_File菜單或Alt控制+ O對於_OK按鈕)。也許有一種方法來防止或重寫呢? – 2010-11-24 19:03:08

回答

23

訣竅是使用SystemKey屬性,如果Key屬性設置爲Key.System

private void ShortcutTextBox_PreviewKeyDown(object sender, KeyEventArgs e) 
{ 
    // The text box grabs all input. 
    e.Handled = true; 

    // Fetch the actual shortcut key. 
    Key key = (e.Key == Key.System ? e.SystemKey : e.Key); 

    // Ignore modifier keys. 
    if (key == Key.LeftShift || key == Key.RightShift 
     || key == Key.LeftCtrl || key == Key.RightCtrl 
     || key == Key.LeftAlt || key == Key.RightAlt 
     || key == Key.LWin || key == Key.RWin) { 
     return; 
    } 

    // Build the shortcut key name. 
    StringBuilder shortcutText = new StringBuilder(); 
    if ((Keyboard.Modifiers & ModifierKeys.Control) != 0) { 
     shortcutText.Append("Ctrl+"); 
    } 
    if ((Keyboard.Modifiers & ModifierKeys.Shift) != 0) { 
     shortcutText.Append("Shift+"); 
    } 
    if ((Keyboard.Modifiers & ModifierKeys.Alt) != 0) { 
     shortcutText.Append("Alt+"); 
    } 
    shortcutText.Append(key.ToString()); 

    // Update the text box. 
    _ShortcutTextBox.Text = shortcutText.ToString(); 
} 

我加Windows鍵修改器列表,因爲它們有時出現在複雜的快捷鍵名稱中(Ctrl+Shift+Alt)組合鍵是從終端服務器會話中鍵入的。不過,它們從來沒有出現在Keyboard.Modifiers中,因爲它們是保留給全局快捷方式的,所以我不在那裏處理它們。

我還使用了StringBuilder,以避免造成過多的string實例。

此解決方案適用於任何組合鍵,但Shift+Alt(在該情況下不會顯示Alt修飾符)。不過,這可能是我的終端服務器環境的一個神器,所以你的里程可能會有所不同。

最後,我添加了一個_File菜單窗口,看看會發生什麼,以及Alt+F快捷鍵有效地在文本框中被困到達菜單,這似乎是你想要的東西了。

+0

+1,比我的解決方案更好。不知道Key key =(e.Key == Key.System?e.SystemKey:e.Key);絕招:) – 2010-11-25 11:15:39

+0

作品!非常感謝! – 2010-11-25 20:12:18

+0

不錯,謝謝分享! – Califf 2013-03-04 22:07:18

-1


如果You used WPF-Command在你的應用程序,你可以使用這個:

<Window.InputBindings> 
    <KeyBinding Command="YourCommnad" 
       Gesture="CTRL+C" /> 
</Window.InputBindings> 
+1

這不能回答這個問題。問題是如何讀取文本框中的組合鍵,而不是如何在XAML中定義熱鍵。 – alexandrudicu 2013-02-07 08:16:03

+1

儘管這並沒有回答原始海報的問題,但事實上,當我通過谷歌搜索偶然發現這個問題時,這就是我正在尋找的答案。所以,想要讓其他人知道,在2016年,這是鍵綁定整個窗口的絕佳解決方案。 – Kikootwo 2016-11-01 13:15:41