2017-02-05 13 views
4

私はwpf c#アプリケーションで作業していますが、ユーザが "/"を押すと検出する必要がありますが、 "/「e.Keyは、私はそのようなKey.OemBackslashと詰め込むがありましたが、私はのための右のイベントを見つけることができません」...OEMキーでスラッシュキーを押すタイミングを検出する方法C#

みんなありがとう、 乾杯

+0

方法e.KeyChar' 'について:

あなたがやろうとしている内容に応じて、あなたはPreviewTextInputイベントを処理したほうが良いかも? –

+0

@SelmanGençw.ffにe.KeyCharがありません。勝利フォームにも存在する私のMessageBoxesと混乱するよりもWindowsフォームライブラリを含める必要があります –

答えて

0

米国キーボードでKey.OemQuestionにする必要があります。しかしスウェーデンのキーボードではそれはD7です。キーボードのキーが必ずしも同じ文字を生成するとは限りません。

protected override void OnPreviewTextInput(TextCompositionEventArgs e) 
{ 
    base.OnPreviewTextInput(e); 
    if (e.Text == "/") 
    { 
     Debug.WriteLine("..."); 
    } 
} 
+0

@私はWPFで作業していますが、このイベントWindow_PreviewKeyDownを使用しています。 –

+0

いいえ、そうでないため、WPFウィンドウのPreviewTextInputをPreviewKeyDownイベントの代わりに処理することができます。そうでなければ、Key.OemQuestionは "/"キーに最も近いです。 – mm8

+0

入力が押されたときを検出する方法はありますか?たとえば、最初にe.Textをチェックし、Enterを押した場合はそのチェックの後に、送信者がKeyEventArgsではなく、TextCompositionEventArgs ..であり、TextCompositionEventArgsにe.Key == Key.Return ..が含まれていない場合はどうすればいいですか? –

1

することができますが(スラッシュ)/」を次のメソッド(see this site)を使用して、キーから文字を取得します。

protected override void OnPreviewKeyDown(KeyEventArgs e) 
{ 
     bool toUnicodeIsTrue=false; 
     char t = GetCharFromKey(e.Key, ref toUnicodeIsTrue); 
     if (t == '/') 
     { 
      // do stuff 
     } 
     base.OnPreviewKeyDown(e); 
} 


    public static char GetCharFromKey(System.Windows.Input.Key key, ref bool toUnicodeIsTrue) 
    { 
     toUnicodeIsTrue = true; 
     char ch = ' '; 

     // First, you need to get the VirtualKey code. Thankfully, there’s a simple class 
     // called KeyInterop, which exposes a static method VirtualKeyFromKey 
     // that gets us this information 
     int virtualKey = System.Windows.Input.KeyInterop.VirtualKeyFromKey(key); 
     //Then, we need to get the character. This is much trickier. 
     //First we have to get the keyboard state and then we have to map that VirtualKey 
     //we got in the first step to a ScanCode, and finally, convert all of that to Unicode, 
     //because .Net doesn’t really speak ASCII 
     byte[] keyboardState = new byte[256]; 
     GetKeyboardState(keyboardState); 

     uint scanCode = MapVirtualKey((uint)virtualKey, MapType.MAPVK_VK_TO_VSC); 
     StringBuilder stringBuilder = new StringBuilder(2); 

     int result = ToUnicode((uint)virtualKey, scanCode, keyboardState, stringBuilder, stringBuilder.Capacity, 0); 
     switch (result) 
     { 
      case -1: 
       toUnicodeIsTrue = false; 
       break; 
      case 0: 
       toUnicodeIsTrue = false; 
       break; 
      case 1: 
       { 
        ch = stringBuilder[0]; 
        break; 
       } 
      default: 
       { 
        ch = stringBuilder[0]; 
        break; 
       } 
     } 
     return ch; 
    } 

    [System.Runtime.InteropServices.DllImport("user32.dll")] 
    public static extern bool GetKeyboardState(byte[] lpKeyState); 
    [System.Runtime.InteropServices.DllImport("user32.dll")] 
    public static extern uint MapVirtualKey(uint uCode, MapType uMapType); 
    public enum MapType : uint 
    { 
     MAPVK_VK_TO_VSC = 0x0, 
     MAPVK_VSC_TO_VK = 0x1, 
     MAPVK_VK_TO_CHAR = 0x2, 
     MAPVK_VSC_TO_VK_EX = 0x3, 
    } 
    [System.Runtime.InteropServices.DllImport("user32.dll")] 
    public static extern int ToUnicode(
     uint wVirtKey, 
     uint wScanCode, 
     byte[] lpKeyState, 
     [System.Runtime.InteropServices.Out, System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr, SizeParamIndex = 4)] 
     StringBuilder pwszBuff, 
     int cchBuff, 
     uint wFlags); 
} 

私の古い回答:キーボードのメーカーを意味名は「OEM」(オリジナル機器メーカー)で始まること

protected override void OnPreviewKeyDown(KeyEventArgs e) 
{ //*** 
    if (e.Key == Key.Oem2) 
    { 
     // do stuff 
    } 
    base.OnPreviewKeyDown(e); 
} 

注意が、その機能のために責任がある、それがローカルに変化キーボード。だから、あなたの中にブレークポイントを設定することができます

{//*** 

私のコードの行とチェックe.Keyプロパティ。

+0

Unforunately Key.Oem2はスラッシュを打つとトリガーされません:( –

+0

私の答えの編集とブレークポイントの設定 – Ron

+0

何も起こっていません.. –

関連する問題