C# keypress

The key press events include KeyDown, KeyUp, KeyPress. Event handling methods are defined in frmMain class and in InitializeComponent() method:

using System.Windows.Forms;
partial class frmMain
{
private void InitializeComponent()
{
this.Width = 1000;
this.Height = 600;
this.KeyPreview = true;
this.KeyPress += new KeyPressEventHandler(testButton_KeyPress);
this.KeyDown += new KeyEventHandler(testButton_KeyDown);
this.FormClosing += new FormClosingEventHandler(testButton_FormClosing);
this.components = new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(frmMain));
this.openDialog = new System.Windows.Forms.OpenFileDialog();
...
}
void testButton_KeyDown(object sender, KeyEventArgs kea)
{
switch (kea.KeyData)
{
case Keys.Left:
...
break;
case Keys.Right:
...
break;
}
}
}

The KeyEventArgs properties include KeyCode, KeyData, KeyValue, KeyModifiers.

List of keycode

:
Key
KeyCode
Key
KeyCode
Key
KeyCode
Backspace (\b)
8
Tab (\t)
9
Enter (\n)
13
Shift
16
Ctrl
17
Alt
18
Pause/Break
19
Caps Lock
20
Esc
27
Page Up
33
Page Down
34
End
35
Home
36
Left ←
37
Up ↑
38
Right →
39
Down ↓
40
Insert
45
Delete
46
0 )
48
1 !
49
2 @
50
3 #
51
4 $
52
5 %
53
6 ^
54
7 &
55
8 *
56
9 (
57
A
65
B
66
C
67
D
68
E
69
F
70
G
71
H
72
I
73
J
74
K
75
L
76
M
77
N
78
O
79
P
80
Q
81
R
82
S
83
T
84
U
85
V
86
W
87
X
88
Y
89
Z
90
Left Win
91
Right Win
92
Context Menu
93
F1
112
F2
113
F3
114
F4
115
F5
116
F6
117
F7
118
F8
119
F9
120
F10
121
F11
122
F12
123
Scroll Lock
145
: ;
186*
+ =
187**
, <
188
- _
189
. >
190
? /
191
~ `
192
{ [
219
| \
220
} ]
221
" '
222

*: 59 in firefox
**: 61 in firefox



Extended Numeric Keyboard:

Key
KeyCode
Key
KeyCode
Key
KeyCode
Num Lock
144
/
111
*
106
7 Home
103/36
8 ↑
104/38
9 PgUp
105/33
4 ←
100/37
5
101/12
6 →
102/39
1 End
97/35
2 ↓
98/40
3 PgDn
99/34
0 Ins
96/45
. Del
110/46
-
109
+
107
Enter
13


endmemo.com © 2024  | Terms of Use | Privacy | Home