2016-06-10 36 views
-1

我需要以下關鍵中的關鍵代碼實際上我想提出限制我的記錄除了粘鍵和功能鍵以外的鍵碼? C#

1 CTRL
2 ALT
3空間
4粘滯鍵如Home和End等
5個功能鍵,如F1 - F12
6 Capslock,scroll,numlock,windows
7點,:,/,\,} {,[] | 〜``

提示:其實我希望只使用AZ,0-9和特殊CHARECTERS只在我的應用程序空格鍵

的例子是,對於JS

if ((vkCode > 48 && vkCode < 91) || (vkCode > 93 && vkCode < 112) || (vkCode > 145) || (vkCode == 32)) 
      { 


     } 

還告訴我當我按下數字鍵它得到具有d 值eaxmple

按下4 --- D4

PRESSING 9 ---- D9

+0

如果您告訴我這個範圍更好 –

回答

0

有在.NET框架枚舉,列出所有的鍵看Keys

,如果你正在處理的關鍵事件,然後它將使用KeyEventArgs

這將在給你活動的徹底破裂鍵盤

private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) 
{ 
    // Initialize the flag to false. 
    nonNumberEntered = false; 

    // Determine whether the keystroke is a number from the top of the keyboard. 
    if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9) 
    { 
     // Determine whether the keystroke is a number from the keypad. 
     if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9) 
     { 
      // Determine whether the keystroke is a backspace. 
      if(e.KeyCode != Keys.Back) 
      { 
       // A non-numerical keystroke was pressed. 
       // Set the flag to true and evaluate in KeyPress event. 
       nonNumberEntered = true; 
      } 
     } 
    } 
    //If shift key was pressed, it's not a number. 
    if (Control.ModifierKeys == Keys.Shift) { 
     nonNumberEntered = true; 
    } 
} 
+0

良好的嘗試,但在下面滿意每一個 –

+0

關掉你的大寫鎖定,其認爲不禮貌 – MikeT

2

這是人們需要

(Keys)vkCode 

A-Z

(Keys)vkCode >= Keys.A && (Keys)vkCode <= Keys.Z) 

TOP NUM 0-9

(vkCode <= 57 && vkCode >= 48) 

NUMPAD NUM

(vkCode <= 105 && vkCode >= 96) 

SPACE

(vkCode == 32) 

ENTER

(vkCode == 13)