2016-04-03 83 views
0

我想將一個簡單的字符串轉換爲一個鍵,我發現了一些解決方案,但其中大部分都是用於winforms,或者他們沒有顯示完整的代碼,所以我沒有明白它。將字符串轉換爲密鑰

這是什麼basicaly我想要實現

namespace KeyDown 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     public string CustomKey = "B"; 
     public MainWindow() 
     { 
      InitializeComponent(); 
      activeArea.Focus(); 
     } 

     private void activeArea_KeyDown(object sender, KeyEventArgs e) 
     { 
      if (e.Key == Key.CustomKey) 
      { 
       MessageBox.Show("Key Pressed"); 
      } 
     } 
    } 
} 

回答

0

可以使用System.Windows.Input.Key枚舉在WPF:

using System.Windows.Input; 

... 

public Key CustomKey = Key.B; 

// or this if you really want to convert the string representation 
public Key CustomKey = (Key)Enum.Parse(typeof(Key), "B"); 


private void activeArea_KeyDown(object sender, KeyEventArgs e) 
{ 
    if (e.Key == CustomKey) 
    { 
     MessageBox.Show("Key Pressed"); 
    } 
} 
+0

謝謝你這個完美的作品! – Simon

+0

@Simon歡迎:) –