键盘响应事件总结


键盘响应事件是在用户按下某个键后触发的事件,可以是任意操作,但不是任意键都可以被捕获的

原型: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

C# 键盘响应事件及键值对照表的更多相关文章

  1. jQuery键盘控制方法,以及键值(keycode)对照表

    键盘控制应用范围非常广泛,比如快捷键控制页面的滚动:在填写表单时候,限制输入内容:或者是屏蔽复制.粘贴.退后等功能.这里说说用jQuery怎么来实现.个人觉得jQuery比原生态的JS好用,代码简单清 ...

  2. js获取键盘按下的键值event.keyCode,event.charCode,event.which的兼容性

    js获取键盘按下的键值有event.keyCode,event.charCode和event.which 其中: 谷歌浏览器对event.keyCode,event.charCode和event.wh ...

  3. mfc 鼠标、键盘响应事件

    一.基本目标 1.有一个基本的MFC程序,点击“关闭”则“关闭”这个程序,这点没什么好讲的,把自带的“取消”按钮,右键->属性的Caption改成“关闭”二字就可以了 2.鼠标在对话框中移动,则 ...

  4. js中键盘按键对应的键值

    js键盘键值 keycode    8 = BackSpace BackSpace  keycode    9 = Tab Tab  keycode   12 = Clear  keycode   1 ...

  5. USB鼠标键盘数据格式以及按键键值

    鼠标发送给PC的数据每次4个字节 BYTE1 BYTE2 BYTE3 BYTE4 定义分别是: BYTE1 --        |--bit7:   1   表示   Y   坐标的变化量超出-256 ...

  6. c# 键值对照表

    虚拟键值表 虚拟键 十六进制值 十进制值 相应键盘或鼠标键 VK_LBUTTON 1 1 鼠标左键 VK_RBUTTON 2 2 鼠标右键 VK_CANCEL 3 3 Ctrl-Break键 VK_M ...

  7. jQuery键盘控制方法,以及键值(keycode)对照表

    键盘控制应用范围非常广泛,比如快捷键控制页面的滚动:在填写表单时候,限制输入内容:或者是屏蔽复制.粘贴.退后等功能.这里说说用jQuery比原生态的JS好用,代码简单清晰,不要问我JS怎么写,因为我不 ...

  8. 键盘虚拟键值编码表 使用keybd_Event

    键盘虚拟键值编码表 使用keybd_Event 模拟键盘输入首先要用到一个API函数:keybd_event. 我们是菜鸟,所以不必具体去理解它的详细用法,只要按以下方法使用即可了!呵呵! 模拟按键有 ...

  9. OpenGL(十六) 鼠标、键盘交互响应事件

    OpenGL中通过鼠标和键盘跟程序交互的实现需要实现注册鼠标和键盘响应事件,在一定条件下,该事件被触发,事件里的程序被执行,达到交互的目的. 通过glutMouseFunc(&OnMouse) ...

随机推荐

  1. 牛客第三场多校 H Diff-prime Pairs

    链接:https://www.nowcoder.com/acm/contest/141/H来源:牛客网 Eddy has solved lots of problem involving calcul ...

  2. netty源码理解补充 之 DefaultChannelPipeline到底是个啥

  3. poj1742(多重背包分解+01背包二进制优化)

    Description People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar. ...

  4. 新建react项目

    npm install -g create-react-app create-react-app my-app cd my-app npm start

  5. webstorm 自动编译ts

    1.在目录根目录添加tsconfig.json { "compileOnSave": false, "compilerOptions": { // 文件目录 & ...

  6. 2017中国大学生程序设计竞赛 - 网络选拔赛 HDU 6154 CaoHaha's staff(几何找规律)

    Problem Description "You shall not pass!"After shouted out that,the Force Staff appered in ...

  7. HDU 1004 Let the Balloon Rise(map应用)

    Problem Description Contest time again! How excited it is to see balloons floating around. But to te ...

  8. HDU 2585 Hotel(字符串的模糊匹配+递归)

    Problem Description Last year summer Max traveled to California for his vacation. He had a great tim ...

  9. 基于链路的OSPFMD5口令认证

    实验要求:掌握基于链路的OSPFMD5口令认证 拓扑如下: 配置如下: R1enable configure terminal interface s0/0/0ip address 192.168.1 ...

  10. 2.26 js解决click失效问题

    2.26 js解决click失效问题 前言有时候元素明明已经找到了,运行也没报错,点击后页面没任何反应.这种问题遇到了,是比较头疼的,因为没任何报错,只是click事件失效了.本篇用2种方法解决这种诡 ...