2016-04-03 17 views
0

シンプルな文字列をキーに変換しようとしていますが、いくつか解決策が見つかりましたが、そのほとんどはwinforms用であり、完全なコードが表示されませんでしたそれを理解します。文字列をキーに変換する

これは私が

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

あなたはWPFでSystem.Windows.Input.Key列挙型を使用することができます達成するために欲しいものbasicalyです:

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

@シモンあなたは大歓迎です:) –

関連する問題