C# 如何捕获键盘按钮和组合键以及KeyPress/KeyDown事件之间的区别 (附KeyChar/KeyCode值)

时间:2021-11-01 20:11:35

1. 首先将窗口属性KeyPreview设为true,如果属性对话框中找不到,就直接在代码里添加; 
2. 添加KeyPress / KeyDown事件:

1、KeyPress 和KeyDown 、KeyPress之间的区别
    1)KeyPress主要用来捕获数字(注意:包括Shift+数字的符号)、字母(注意:包括大小写)、小键盘等除了F1-12、SHIFT、Alt、Ctrl、Insert、Home、PgUp、Delete、End、PgDn、ScrollLock、Pause、NumLock、{菜单键}、{开始键}和方向键外的ANSI字符 
        KeyDown 和KeyUp 通常可以捕获键盘除了PrScrn所有按键(这里不讨论特殊键盘的特殊键) 
    2)KeyPress 只能捕获单个字符 
        KeyDown 和 KeyUp 可以捕获组合键。 
    3)KeyPress 可以捕获单个字符的大小写 
    4)KeyDown和KeyUp 对于单个字符捕获的KeyValue 都是一个值,也就是不能判断单个字符的大小写。 
    5)KeyPress 不区分小键盘和主键盘的数字字符。 
        KeyDown 和KeyUp 区分小键盘和主键盘的数字字符。 
    6)其中PrScrn 按键KeyPress、KeyDown和KeyUp 都不能捕获。

2、KeyPress事件

Winform文本框只能输入数字和退格键和小数点:

private void TextBox_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar != && !Char.IsDigit(e.KeyChar) && e.KeyChar != '.')
{
e.Handled = true;
}
}
//或者
private void TextBox_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar != '\b' && !Char.IsDigit(e.KeyChar) && e.KeyChar != '.')
{
e.Handled = true;
}
}

3、KeyChar取值:

C# 如何捕获键盘按钮和组合键以及KeyPress/KeyDown事件之间的区别 (附KeyChar/KeyCode值)

另外2个特殊ASCII码:回车=13,换行=10,退格='\b' 或者 退格=8

4、KeyDown事件:

捕获单个按键:

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
Keys key = e.KeyCode;
if (e.Control!=true)//如果没按Ctrl键
return;
switch (key)
{
case Keys.NumPad0:
//按下小键盘0以后
break;
case Keys.NumPad1:
//按下小键盘1以后
break;
case Keys.S:
//按下S键以后
break;
case Keys.Up:
//按下向下键以后
break;
}
}

捕获组合键(以全选为例):

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.A)
{
(sender as TextBox).SelectAll();
}
}

5、KeyCode取值: 
keycode    8 = BackSpace BackSpace 
keycode    9 = Tab Tab 
keycode   12 = Clear 
keycode   13 = Enter 
keycode   16 = Shift_L 
keycode   17 = Control_L 
keycode   18 = Alt_L 
keycode   19 = Pause 
keycode   20 = Caps_Lock 
keycode   27 = Escape Escape 
keycode   32 = space space 
keycode   33 = Prior 
keycode   34 = Next 
keycode   35 = End 
keycode   36 = Home 
keycode   37 = Left 
keycode   38 = Up 
keycode   39 = Right 
keycode   40 = Down 
keycode   41 = Select 
keycode   42 = Print 
keycode   43 = Execute 
keycode   45 = Insert 
keycode   46 = Delete 
keycode   47 = Help 
keycode   48 = 0 equal braceright 
keycode   49 = 1 exclam onesuperior 
keycode   50 = 2 quotedbl twosuperior 
keycode   51 = 3 section threesuperior 
keycode   52 = 4 dollar 
keycode   53 = 5 percent 
keycode   54 = 6 ampersand 
keycode   55 = 7 slash braceleft 
keycode   56 = 8 parenleft bracketleft 
keycode   57 = 9 parenright bracketright 
keycode   65 = a A 
keycode   66 = b B 
keycode   67 = c C 
keycode   68 = d D 
keycode   69 = e E EuroSign 
keycode   70 = f F 
keycode   71 = g G 
keycode   72 = h H 
keycode   73 = i I 
keycode   74 = j J 
keycode   75 = k K 
keycode   76 = l L 
keycode   77 = m M mu 
keycode   78 = n N 
keycode   79 = o O 
keycode   80 = p P 
keycode   81 = q Q at 
keycode   82 = r R 
keycode   83 = s S 
keycode   84 = t T 
keycode   85 = u U 
keycode   86 = v V 
keycode   87 = w W 
keycode   88 = x X 
keycode   89 = y Y 
keycode   90 = z Z 
keycode   96 = KP_0 KP_0 
keycode   97 = KP_1 KP_1 
keycode   98 = KP_2 KP_2 
keycode   99 = KP_3 KP_3 
keycode 100 = KP_4 KP_4 
keycode 101 = KP_5 KP_5 
keycode 102 = KP_6 KP_6 
keycode 103 = KP_7 KP_7 
keycode 104 = KP_8 KP_8 
keycode 105 = KP_9 KP_9 
keycode 106 = KP_Multiply KP_Multiply 
keycode 107 = KP_Add KP_Add 
keycode 108 = KP_Separator KP_Separator 
keycode 109 = KP_Subtract KP_Subtract 
keycode 110 = KP_Decimal KP_Decimal 
keycode 111 = KP_Divide KP_Divide 
keycode 112 = F1 
keycode 113 = F2 
keycode 114 = F3 
keycode 115 = F4 
keycode 116 = F5 
keycode 117 = F6 
keycode 118 = F7 
keycode 119 = F8 
keycode 120 = F9 
keycode 121 = F10 
keycode 122 = F11 
keycode 123 = F12 
keycode 124 = F13 
keycode 125 = F14 
keycode 126 = F15 
keycode 127 = F16 
keycode 128 = F17 
keycode 129 = F18 
keycode 130 = F19 
keycode 131 = F20 
keycode 132 = F21 
keycode 133 = F22 
keycode 134 = F23 
keycode 135 = F24 
keycode 136 = Num_Lock 
keycode 137 = Scroll_Lock 
keycode 187 = acute grave 
keycode 188 = comma semicolon 
keycode 189 = minus underscore 
keycode 190 = period colon 
keycode 192 = numbersign apostrophe 
keycode 210 = plusminus hyphen macron 
keycode 211 = 
keycode 212 = copyright registered 
keycode 213 = guillemotleft guillemotright 
keycode 214 = masculine ordfeminine 
keycode 215 = ae AE 
keycode 216 = cent yen 
keycode 217 = questiondown exclamdown 
keycode 218 = onequarter onehalf threequarters 
keycode 220 = less greater bar 
keycode 221 = plus asterisk asciitilde 
keycode 227 = multiply division 
keycode 228 = acircumflex Acircumflex 
keycode 229 = ecircumflex Ecircumflex 
keycode 230 = icircumflex Icircumflex 
keycode 231 = ocircumflex Ocircumflex 
keycode 232 = ucircumflex Ucircumflex 
keycode 233 = ntilde Ntilde 
keycode 234 = yacute Yacute 
keycode 235 = oslash Ooblique 
keycode 236 = aring Aring 
keycode 237 = ccedilla Ccedilla 
keycode 238 = thorn THORN 
keycode 239 = eth ETH 
keycode 240 = diaeresis cedilla currency 
keycode 241 = agrave Agrave atilde Atilde 
keycode 242 = egrave Egrave 
keycode 243 = igrave Igrave 
keycode 244 = ograve Ograve otilde Otilde 
keycode 245 = ugrave Ugrave 
keycode 246 = adiaeresis Adiaeresis 
keycode 247 = ediaeresis Ediaeresis 
keycode 248 = idiaeresis Idiaeresis 
keycode 249 = odiaeresis Odiaeresis 
keycode 250 = udiaeresis Udiaeresis 
keycode 251 = ssharp question backslash 
keycode 252 = asciicircum degree 
keycode 253 = 3 sterling 
keycode 254 = Mode_switch

6、如何获取键盘按钮的keyChar值(keyCode同理)

private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
char Key_Char = e.KeyChar;//判断按键的 Keychar
MessageBox.Show(((int)(Key_Char)).ToString());//转成整数显示
}