C# 键盘响应事件及键值对照表
键盘响应事件总结
键盘响应事件是在用户按下某个键后触发的事件,可以是任意操作,但不是任意键都可以被捕获的。
原型:public event KeyPressEventHandler KeyPress
MSDN说明:键事件按以下顺序发生:
1.KeyDown
2.KeyPress
3.KeyUp
KeyPress 事件不能由非字符键引发;但是非字符键能够引发 KeyDown 和 KeyUp事件。
使用 KeyChar 属性采样运行时的键击,以及使用或修改常用键击的一个子集。
若要仅在窗体级别处理键盘事件而不允许其他控件接收键盘事件,请将窗体的 KeyPress 事件处理方法中的KeyPressEventArgs.Handled 属性设置为 true。MSDN
下面的代码示例使用 KeyPress事件来禁止向控件输入字符。
// Boolean flag used to determine when a character other than a number is entered.
private bool nonNumberEntered = false;
// Handle the KeyDown event to determine the type of character entered into the control.
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;
}
}
// This event occurs after the KeyDown event and can be used to prevent
// characters from entering the control.
private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
// Check for the flag being set in the KeyDown event.
if (nonNumberEntered == true)
{
// Stop the character from being entered into the control since it is non-numerical.
e.Handled = true;
}
}
键值对照表
虚拟键值表 | |||
虚拟键 | 十六进制值 | 十进制值 | 相应键盘或鼠标键 |
VK_LBUTTON | 1 | 1 | 鼠标左键 |
VK_RBUTTON | 2 | 2 | 鼠标右键 |
VK_CANCEL | 3 | 3 | Ctrl-Break键 |
VK_MBUTTON | 4 | 4 | 鼠标中键 |
VK_BACK | 8 | 8 | Backspace键 |
VK_TAB | 9 | 9 | Tab键 |
VK_CLEAR | 0C | 12 | Clear键 |
VK_RETURN | 0D | 13 | Enter键 |
VK_SHIFT | 10 | 16 | Shift键 |
VK_CONTROL | 11 | 17 | Ctrl键 |
VK_MENU | 12 | 18 | Alt键 |
VK_PAUSE | 13 | 19 | Pause键 |
VK_CAPITAL | 14 | 20 | Caps Lock键 |
VK_ESCAPE | 1B | 27 | Esc键 |
VK_SPACE | 20 | 32 | Space键 |
VK_PRIOR | 21 | 33 | Page Up键 |
VK_NEXT | 22 | 34 | Page Down键 |
VK_END | 23 | 35 | End键 |
VK_HOME | 24 | 36 | Home键 |
VK_LEFT | 25 | 37 | ←键 |
VK_UP | 26 | 38 | ↑键 |
VK_RIGHT | 27 | 39 | →键 |
VK_DOWN | 28 | 40 | ↓键 |
VK_SELECT | 29 | 41 | Select键 |
VK_PRINT | 2A | 42 | Print键 |
VK_EXECUTE | 2B | 43 | Execute键 |
VK_SNAPSHOT | 2C | 44 | Print Screen键 |
VK_INSERT | 2D | 45 | Ins键 |
VK_DELETE | 2E | 46 | Del键 |
VK_HELP | 2F | 47 | Help键 |
VK_0 | 30 | 48 | 0键 |
VK_1 | 31 | 49 | 1键 |
VK_2 | 32 | 50 | 2键 |
VK_3 | 33 | 51 | 3键 |
VK_4 | 34 | 52 | 4键 |
VK_5 | 35 | 53 | 5键 |
VK_6 | 36 | 54 | 6键 |
VK_7 | 37 | 55 | 7键 |
VK_8 | 38 | 56 | 8键 |
VK_9 | 39 | 57 | 9键 |
VK_A | 41 | 65 | A键 |
VK_B | 42 | 66 | B键 |
VK_C | 43 | 67 | C键 |
VK_D | 44 | 68 | D键 |
VK_E | 45 | 69 | E键 |
VK_F | 46 | 70 | F键 |
VK_G | 47 | 71 | G键 |
VK_H | 48 | 72 | H键 |
VK_I | 49 | 73 | I键 |
VK_J | 4A | 74 | J键 |
VK_K | 4B | 75 | K键 |
VK_L | 4C | 76 | L键 |
VK_M | 4D | 77 | M键 |
VK_N | 4E | 78 | N键 |
VK_O | 4F | 79 | O键 |
VK_P | 50 | 80 | P键 |
VK_Q | 51 | 81 | Q键 |
VK_R | 52 | 82 | R键 |
VK_S | 53 | 83 | S键 |
VK_T | 54 | 84 | T键 |
VK_U | 55 | 85 | U键 |
VK_V | 56 | 86 | V键 |
VK_W | 57 | 87 | W键 |
VK_X | 58 | 88 | X键 |
VK_Y | 59 | 89 | Y键 |
VK_Z | 5A | 90 | Z键 |
VK_LWIN | 5B | 91 | 左Windows键 |
VK_RWIN | 5C | 92 | 右Windows键 |
VK_APPS | 5D | 93 | 应用程序键 |
VK_SLEEP | 5F | 95 | 休眠键 |
VK_NUMPAD0 | 60 | 96 | 小数字键盘0键 |
VK_NUMPAD1 | 61 | 97 | 小数字键盘1键 |
VK_NUMPAD2 | 62 | 98 | 小数字键盘2键 |
VK_NUMPAD3 | 63 | 99 | 小数字键盘3键 |
VK_NUMPAD4 | 64 | 100 | 小数字键盘4键 |
VK_NUMPAD5 | 65 | 101 | 小数字键盘5键 |
VK_NUMPAD6 | 66 | 102 | 小数字键盘6键 |
VK_NUMPAD7 | 67 | 103 | 小数字键盘7键 |
VK_NUMPAD8 | 68 | 104 | 小数字键盘8键 |
VK_NUMPAD9 | 69 | 105 | 小数字键盘9键 |
VK_MULTIPLY | 6A | 106 | 乘号键 |
VK_ADD | 6B | 107 | 加号键 |
VK_SEPARATOR | 6C | 108 | 分割键 |
VK_SUBSTRACT | 6D | 109 | 减号键 |
VK_DECIMAL | 6E | 110 | 小数点键 |
VK_DIVIDE | 6F | 111 | 除号键 |
VK_F1 | 70 | 12 | F1键 |
VK_F2 | 71 | 113 | F2键 |
VK_F3 | 72 | 114 | F3键 |
VK_F4 | 73 | 115 | F4键 |
VK_F5 | 74 | 116 | F5键 |
VK_F6 | 75 | 117 | F6键 |
VK_F7 | 76 | 118 | F7键 |
VK_F8 | 77 | 119 | F8键 |
VK_F9 | 78 | 120 | F9键 |
VK_F10 | 79 | 121 | F10键 |
VK_F11 | 7A | 122 | F11键 |
VK_F12 | 7B | 123 | F12键 |
VK_F13 | 7C | 124 | F13键 |
VK_F14 | 7D | 125 | F14键 |
VK_F15 | 7E | 126 | F15键 |
VK_F16 | 7F | 127 | F16键 |
VK_F17 | 80 | 128 | F17键 |
VK_F18 | 81 | 129 | F18键 |
VK_F19 | 82 | 130 | F19键 |
VK_F20 | 83 | 131 | F20键 |
VK_F21 | 84 | 132 | F21键 |
VK_F22 | 85 | 133 | F22键 |
VK_F23 | 86 | 134 | F23键 |
VK_F24 | 87 | 135 | F24键 |
VK_NUMLOCK | 90 | 144 | Num Lock键 |
VK_SCROLL | 91 | 45 | Scroll Lock键 |
VK_LSHIFT | A0 | 160 | 左Shift键 |
VK_RSHIFT | A1 | 161 | 右Shift键 |
VK_LCONTROL | A2 | 162 | 左Ctrl键 |
VK_RCONTROL | A3 | 163 | 右Ctrl键 |
VK_LMENU | A4 | 164 | 左Alt键 |
VK_RMENU | A5 | 165 | 右Alt键 |
PS:以上表格的导入如果使用Makedown语法显然不太合适,这里我使用表格自动转换HTML语法的网站,非常方便,链接如下:在线HTML格式转换
出处:https://blog.csdn.net/u012391923/article/details/52920316
参考:https://www.cnblogs.com/swtool/p/6860760.html
关注我】。(●'◡'●)
如果,您希望更容易地发现我的新博客,不妨点击一下绿色通道的【因为,我的写作热情也离不开您的肯定与支持,感谢您的阅读,我是【Jack_孟】!
本文来自博客园,作者:jack_Meng,转载请注明原文链接:https://www.cnblogs.com/mq0036/p/10416623.html
【免责声明】本文来自源于网络,如涉及版权或侵权问题,请及时联系我们,我们将第一时间删除或更改!
posted on 2019-02-22 09:58 jack_Meng 阅读(3651) 评论(0) 编辑 收藏 举报