2017-06-16 3 views
-1

KeyDownイベントを使用してTextBoxで特定の文字のみを受け入れるようにします。私はすでに1文字、一重引用符を除いて、それを動作させました。書かれるキャラクタを得るために、私は(char)e.KeyValueを使用します。これは引用符を除くすべての文字に対して機能します(Ûを付ける)。私はただe.KeyCodeを使用することができますが、値はKeys.Oem4であり、AFAIKはシステムによって異なる可能性があります。KeyDownイベントで一重引用符キーを検出する

一重引用符キーを一貫して検出する方法はありますか?

は、コードスニペット:私のために

char c = (char)e.KeyValue; 
char[] moves = { 'r', 'u', ..., '\'' }; 

if (!(moves.Contains(c) || e.KeyCode == Keys.Back || e.KeyCode == Keys.Space)) 
{ 
    e.SuppressKeyPress = true; 
} 
+0

'KeyPress'イベント(と思う[WM_CHARである](https://msdn.microsoft.com/en-us/library/windows/desktop/ms646276(V = VS.85).aspxの))は 'KeyPressEventArgs'を使用します。これはあなたが探しているものである' KeyChar'プロパティを持っています。そのイベントが必要なことをするなら、それを使うことができます。 –

+0

@EdPlunkettが言ったこと。 'KeyDown'と' KeyEventArgs'は、実際の文字ではなく、仮想キーコードだけを与えます(値が一致するので、キャストの動作はちょうど偶然です)。単一引用符を生成するキーの仮想キーコードは、レイアウトによって異なります。問題は 'KeyCode'ではありません。 'KeyValue'も普遍的ではありません。 –

+0

@EdPlunkettそれはそうしますが、私はキープレスを抑制する必要があります。これは 'KeyDown'イベントでのみ可能です。 – Pipe

答えて

-1

@EdPlunkettが示唆したように、this answer作品:私は長い時間のためにこれを使用している

[DllImport("user32.dll")] 
static extern bool GetKeyboardState(byte[] lpKeyState); 

[DllImport("user32.dll")] 
static extern uint MapVirtualKey(uint uCode, uint uMapType); 

[DllImport("user32.dll")] 
static extern IntPtr GetKeyboardLayout(uint idThread); 

[DllImport("user32.dll")] 
static extern int ToUnicodeEx(uint wVirtKey, uint wScanCode, byte[] lpKeyState, [Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pwszBuff, int cchBuff, uint wFlags, IntPtr dwhkl); 


public static string KeyCodeToUnicode(System.Windows.Forms.Keys key) 
{ 
    byte[] keyboardState = new byte[255]; 
    bool keyboardStateStatus = GetKeyboardState(keyboardState); 

    if (!keyboardStateStatus) 
    { 
     return ""; 
    } 

    uint virtualKeyCode = (uint)key; 
    uint scanCode = MapVirtualKey(virtualKeyCode, 0); 
    IntPtr inputLocaleIdentifier = GetKeyboardLayout(0); 

    StringBuilder result = new StringBuilder(); 
    ToUnicodeEx(virtualKeyCode, scanCode, keyboardState, result, (int)5, (uint)0, inputLocaleIdentifier); 

    return result.ToString(); 
} 
1

。これは一重引用符をうまく処理します。 e.KeyChar == 39 '\' 'とe.Handled = trueは、あなたが期待する通りに正確に動作します。私はKeyPressイベントでそれをテストし、そこでも動作します。

protected override void OnKeyPress(KeyPressEventArgs e) 
    { 
     base.OnKeyPress(e); 
     if (e.KeyChar == (char)8) // backspace 
      return; 
     if (e.KeyChar == (char)3) // ctrl + c 
      return; 
     if (e.KeyChar == (char)22) // ctrl + v 
      return; 
     typedkey = true; 
     if (_allowedCharacters.Count > 0) // if the string of allowed characters is not empty, skip test if empty 
     { 
      if (!_allowedCharacters.Contains(e.KeyChar)) // if the new character is not in allowed set, 
      { 
       e.Handled = true; // ignoring it 
       return; 
      } 
     } 
     if (_disallowedCharacters.Count > 0) // if the string of allowed characters is not empty, skip test if empty 
     { 
      if (_disallowedCharacters.Contains(e.KeyChar)) // if the new character is in disallowed set, 
      { 
       e.Handled = true; // ignoring it 
       return; 
      } 
     } 
    } 
関連する問題