键盘太脏了,洗洗吧。。。。。。。。。。。。一个一个扣啊,扣完洗啊。。。。。。。。
OK!搞定了,装上去试试,我勒个去,TAB坏了,咋办,用ENTER键当TAB用吧(实际不会这么做滴,呵呵)。好了,编码实现~~
首先我们讲的是winform程序,其他语言请勿对号入座,如有雷同,纯属巧合~
言归正传,ACTION~
其实监控键盘按键有两种方式:
1、重写ProcessDialogKey方法:
官方是这样解释ProcessDialogKey方法的
在消息预处理过程中调用此方法,以处理对话框字符,比如 Tab、Return、Esc 和箭头键。只有当 IsInputKey 方法指示控件未在处理该键时,才调用此方法。ProcessDialogKey 方法只是将字符发送给父级的 ProcessDialogKey 方法,或者,在该控件没有父级的情况下返回 false。Form 类重写此方法来执行对话框键的实际处理。仅当控件承载在 Windows 窗体应用程序中或充当 ActiveX 控件时,才调用此方法。
给继承者的说明 在派生类中重写 ProcessDialogKey 方法时,控件应返回 true 以指示它已处理该键。对于未由该控件处理的键,应返回调用基类的ProcessDialogChar 方法所得的结果。控件很少需要重写此方法(即使有的话)。
2、给控件增加KeyDown事件
代码如下:
1 protected override bool ProcessDialogKey(Keys keyData)
2 {
3 if (keyData == Keys.Return)
4 {
5 return base.ProcessDialogKey(Keys.Tab);
6 }
7 else
8 {
9 return base.ProcessDialogKey(keyData);
10 }
11 }
1 foreach(Control ct in groupBox1.Controls)
2 {
3 if ((ct is TextBox) || (ct is ComboBox))
4
5 {
6 ct.KeyDown += new System.Windows.Forms.KeyEventHandler(this.EnterKeyDown);
7 }
8 }
9
10
11 private void EnterKeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
12 {
13 if(e.KeyCode==Keys.Enter)
14 {
15 System.Windows.Forms.SendKeys.Send("{TAB}");
16 }
17 }
好了,这样我们就将回车键替换为了TAB,大家试试看,好用不。
最后附上keycode对应如下:
1 keycode 8 = BackSpace BackSpace
2 keycode 9 = Tab Tab
3 keycode 12 = Clear
4 keycode 13 = Enter
5 keycode 16 = Shift_L
6 keycode 17 = Control_L
7 keycode 18 = Alt_L
8 keycode 19 = Pause
9 keycode 20 = Caps_Lock
10 keycode 27 = Escape Escape
11 keycode 32 = space space
12 keycode 33 = Prior
13 keycode 34 = Next
14 keycode 35 = End
15 keycode 36 = Home
16 keycode 37 = Left
17 keycode 38 = Up
18 keycode 39 = Right
19 keycode 40 = Down
20 keycode 41 = Select
21 keycode 42 = Print
22 keycode 43 = Execute
23 keycode 45 = Insert
24 keycode 46 = Delete
25 keycode 47 = Help
26 keycode 48 = 0 equal braceright
27 keycode 49 = 1 exclam onesuperior
28 keycode 50 = 2 quotedbl twosuperior
29 keycode 51 = 3 section threesuperior
30 keycode 52 = 4 dollar
31 keycode 53 = 5 percent
32 keycode 54 = 6 ampersand
33 keycode 55 = 7 slash braceleft
34 keycode 56 = 8 parenleft bracketleft
35 keycode 57 = 9 parenright bracketright
36 keycode 65 = a A
37 keycode 66 = b B
38 keycode 67 = c C
39 keycode 68 = d D
40 keycode 69 = e E EuroSign
41 keycode 70 = f F
42 keycode 71 = g G
43 keycode 72 = h H
44 keycode 73 = i I
45 keycode 74 = j J
46 keycode 75 = k K
47 keycode 76 = l L
48 keycode 77 = m M mu
49 keycode 78 = n N
50 keycode 79 = o O
51 keycode 80 = p P
52 keycode 81 = q Q at
53 keycode 82 = r R
54 keycode 83 = s S
55 keycode 84 = t T
56 keycode 85 = u U
57 keycode 86 = v V
58 keycode 87 = w W
59 keycode 88 = x X
60 keycode 89 = y Y
61 keycode 90 = z Z
62 keycode 96 = KP_0 KP_0
63 keycode 97 = KP_1 KP_1
64 keycode 98 = KP_2 KP_2
65 keycode 99 = KP_3 KP_3
66 keycode 100 = KP_4 KP_4
67 keycode 101 = KP_5 KP_5
68 keycode 102 = KP_6 KP_6
69 keycode 103 = KP_7 KP_7
70 keycode 104 = KP_8 KP_8
71 keycode 105 = KP_9 KP_9
72 keycode 106 = KP_Multiply KP_Multiply
73 keycode 107 = KP_Add KP_Add
74 keycode 108 = KP_Separator KP_Separator
75 keycode 109 = KP_Subtract KP_Subtract
76 keycode 110 = KP_Decimal KP_Decimal
77 keycode 111 = KP_Divide KP_Divide
78 keycode 112 = F1
79 keycode 113 = F2
80 keycode 114 = F3
81 keycode 115 = F4
82 keycode 116 = F5
83 keycode 117 = F6
84 keycode 118 = F7
85 keycode 119 = F8
86 keycode 120 = F9
87 keycode 121 = F10
88 keycode 122 = F11
89 keycode 123 = F12
90 keycode 124 = F13
91 keycode 125 = F14
92 keycode 126 = F15
93 keycode 127 = F16
94 keycode 128 = F17
95 keycode 129 = F18
96 keycode 130 = F19
97 keycode 131 = F20
98 keycode 132 = F21
99 keycode 133 = F22
100 keycode 134 = F23
101 keycode 135 = F24
102 keycode 136 = Num_Lock
103 keycode 137 = Scroll_Lock
104 keycode 187 = acute grave
105 keycode 188 = comma semicolon
106 keycode 189 = minus underscore
107 keycode 190 = period colon
108 keycode 192 = numbersign apostrophe
109 keycode 210 = plusminus hyphen macron
110 keycode 211 =
111 keycode 212 = copyright registered
112 keycode 213 = guillemotleft guillemotright
113 keycode 214 = masculine ordfeminine
114 keycode 215 = ae AE
115 keycode 216 = cent yen
116 keycode 217 = questiondown exclamdown
117 keycode 218 = onequarter onehalf threequarters
118 keycode 220 = less greater bar
119 keycode 221 = plus asterisk asciitilde
120 keycode 227 = multiply division
121 keycode 228 = acircumflex Acircumflex
122 keycode 229 = ecircumflex Ecircumflex
123 keycode 230 = icircumflex Icircumflex
124 keycode 231 = ocircumflex Ocircumflex
125 keycode 232 = ucircumflex Ucircumflex
126 keycode 233 = ntilde Ntilde
127 keycode 234 = yacute Yacute
128 keycode 235 = oslash Ooblique
129 keycode 236 = aring Aring
130 keycode 237 = ccedilla Ccedilla
131 keycode 238 = thorn THORN
132 keycode 239 = eth ETH
133 keycode 240 = diaeresis cedilla currency
134 keycode 241 = agrave Agrave atilde Atilde
135 keycode 242 = egrave Egrave
136 keycode 243 = igrave Igrave
137 keycode 244 = ograve Ograve otilde Otilde
138 keycode 245 = ugrave Ugrave
139 keycode 246 = adiaeresis Adiaeresis
140 keycode 247 = ediaeresis Ediaeresis
141 keycode 248 = idiaeresis Idiaeresis
142 keycode 249 = odiaeresis Odiaeresis
143 keycode 250 = udiaeresis Udiaeresis
144 keycode 251 = ssharp question backslash
145 keycode 252 = asciicircum degree
146 keycode 253 = 3 sterling
147 keycode 254 = Mode_switch