Keys 枚举
指定键代码和修饰符。
此枚举有一个 FlagsAttribute 属性,允许其成员值按位组合。
**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)
语法
声明
<ComVisibleAttribute(True)> _
<FlagsAttribute> _
Public Enumeration Keys
用法
Dim instance As Keys
[ComVisibleAttribute(true)]
[FlagsAttribute]
public enum Keys
[ComVisibleAttribute(true)]
[FlagsAttribute]
public enum class Keys
/** @attribute ComVisibleAttribute(true) */
/** @attribute FlagsAttribute() */
public enum Keys
ComVisibleAttribute(true)
FlagsAttribute
public enum Keys
成员
成员名称 | 说明 | |
---|---|---|
A | A 键。 | |
Add | 加号键。 | |
Alt | Alt 修改键。 | |
Apps | 应用程序键(Microsoft Natural Keyboard,人体工程学键盘)。 | |
Attn | Attn 键。 | |
B | B 键。 | |
Back | Backspace 键。 | |
BrowserBack | 浏览器后退键(Windows 2000 或更高版本)。 | |
BrowserFavorites | 浏览器收藏夹键(Windows 2000 或更高版本)。 | |
BrowserForward | 浏览器前进键(Windows 2000 或更高版本)。 | |
BrowserHome | 浏览器主页键(Windows 2000 或更高版本)。 | |
BrowserRefresh | 浏览器刷新键(Windows 2000 或更高版本)。 | |
BrowserSearch | 浏览器搜索键(Windows 2000 或更高版本)。 | |
BrowserStop | 浏览器停止键(Windows 2000 或更高版本)。 | |
C | C 键。 | |
Cancel | Cancel 键。 | |
Capital | Caps Lock 键。 | |
CapsLock | Caps Lock 键。 | |
Clear | Clear 键。 | |
Control | Ctrl 修改键。 | |
ControlKey | Ctrl 键。 | |
Crsel | Crsel 键。 | |
D | D 键。 | |
D0 | 0 键。 | |
D1 | 1 键。 | |
D2 | 2 键。 | |
D3 | 3 键。 | |
D4 | 4 键。 | |
D5 | 5 键。 | |
D6 | 6 键。 | |
D7 | 7 键。 | |
D8 | 8 键。 | |
D9 | 9 键。 | |
Decimal | 句点键。 | |
Delete | DeL 键。 | |
Divide | 除号键。 | |
Down | 向下键。 | |
E | E 键。 | |
End | End 键。 | |
Enter | Enter 键。 | |
EraseEof | ERASE EOF 键。 | |
Escape | Esc 键。 | |
Execute | EXECUTE 键。 | |
Exsel | Exsel 键。 | |
F | F 键。 | |
F1 | F1 键。 | |
F10 | F10 键。 | |
F11 | F11 键。 | |
F12 | F12 键。 | |
F13 | F13 键。 | |
F14 | F14 键。 | |
F15 | F15 键。 | |
F16 | F16 键。 | |
F17 | F17 键。 | |
F18 | F18 键。 | |
F19 | F19 键。 | |
F2 | F2 键。 | |
F20 | F20 键。 | |
F21 | F21 键。 | |
F22 | F22 键。 | |
F23 | F23 键。 | |
F24 | F24 键。 | |
F3 | F3 键。 | |
F4 | F4 键。 | |
F5 | F5 键。 | |
F6 | F6 键。 | |
F7 | F7 键。 | |
F8 | F8 键。 | |
F9 | F9 键。 | |
FinalMode | IME 最终模式键。 | |
G | G 键。 | |
H | H 键。 | |
HanguelMode | IME Hanguel 模式键。(为了保持兼容性而设置;使用 HangulMode) | |
HangulMode | IME Hangul 模式键。 | |
HanjaMode | IME Hanja 模式键。 | |
Help | Help 键。 | |
Home | Home 键。 | |
I | I 键。 | |
IMEAccept | IME 接受键,替换 IMEAceept。 | |
IMEAceept | IME 接受键。已过时,请改用 IMEAccept。 | |
IMEConvert | IME 转换键。 | |
IMEModeChange | IME 模式更改键。 | |
IMENonconvert | IME 非转换键。 | |
Insert | Ins 键。 | |
J | J 键。 | |
JunjaMode | IME Junja 模式键。 | |
K | K 键。 | |
KanaMode | IME Kana 模式键。 | |
KanjiMode | IME Kanji 模式键。 | |
KeyCode | 从键值提取键代码的位屏蔽。 | |
L | L 键。 | |
LaunchApplication1 | 启动应用程序一键(Windows 2000 或更高版本)。 | |
LaunchApplication2 | 启动应用程序二键(Windows 2000 或更高版本)。 | |
LaunchMail | 启动邮件键(Windows 2000 或更高版本)。 | |
LButton | 鼠标左按钮。 | |
LControlKey | 左 Ctrl 键。 | |
Left | 向左键。 | |
LineFeed | LINEFEED 键。 | |
LMenu | 左 Alt 键。 | |
LShiftKey | 左 Shift 键。 | |
LWin | 左 Windows 徽标键(Microsoft Natural Keyboard,人体工程学键盘)。 | |
M | M 键。 | |
MButton | 鼠标中按钮(三个按钮的鼠标)。 | |
MediaNextTrack | 媒体下一曲目键(Windows 2000 或更高版本)。 | |
MediaPlayPause | 媒体播放暂停键(Windows 2000 或更高版本)。 | |
MediaPreviousTrack | 媒体上一曲目键(Windows 2000 或更高版本)。 | |
MediaStop | 媒体停止键(Windows 2000 或更高版本)。 | |
Menu | Alt 键。 | |
Modifiers | 从键值提取修饰符的位屏蔽。 | |
Multiply | 乘号键。 | |
N | N 键。 | |
Next | Page Down 键。 | |
NoName | 保留以备将来使用的常数。 | |
None | 没有按任何键。 | |
NumLock | Num Lock 键。 | |
NumPad0 | 数字键盘上的 0 键。 | |
NumPad1 | 数字键盘上的 1 键。 | |
NumPad2 | 数字键盘上的 2 键。 | |
NumPad3 | 数字键盘上的 3 键。 | |
NumPad4 | 数字键盘上的 4 键。 | |
NumPad5 | 数字键盘上的 5 键。 | |
NumPad6 | 数字键盘上的 6 键。 | |
NumPad7 | 数字键盘上的 7 键。 | |
NumPad8 | 数字键盘上的 8 键。 | |
NumPad9 | 数字键盘上的 9 键。 | |
O | O 键。 | |
Oem1 | OEM 1 键。 | |
Oem102 | OEM 102 键。 | |
Oem2 | OEM 2 键。 | |
Oem3 | OEM 3 键。 | |
Oem4 | OEM 4 键。 | |
Oem5 | OEM 5 键。 | |
Oem6 | OEM 6 键。 | |
Oem7 | OEM 7 键。 | |
Oem8 | OEM 8 键。 | |
OemBackslash | RT 102 键的键盘上的 OEM 尖括号或反斜杠键(Windows 2000 或更高版本)。 | |
OemClear | Clear 键。 | |
OemCloseBrackets | 美式标准键盘上的 OEM 右括号键(Windows 2000 或更高版本)。 | |
Oemcomma | 任何国家/地区键盘上的 OEM 逗号键(Windows 2000 或更高版本)。 | |
OemMinus | 任何国家/地区键盘上的 OEM 减号键(Windows 2000 或更高版本)。 | |
OemOpenBrackets | 美式标准键盘上的 OEM 左括号键(Windows 2000 或更高版本)。 | |
OemPeriod | 任何国家/地区键盘上的 OEM 句点键(Windows 2000 或更高版本)。 | |
OemPipe | 美式标准键盘上的 OEM 管道键(Windows 2000 或更高版本)。 | |
Oemplus | 任何国家/地区键盘上的 OEM 加号键(Windows 2000 或更高版本)。 | |
OemQuestion | 美式标准键盘上的 OEM 问号键(Windows 2000 或更高版本)。 | |
OemQuotes | 美式标准键盘上的 OEM 单/双引号键(Windows 2000 或更高版本)。 | |
OemSemicolon | 美式标准键盘上的 OEM 分号键(Windows 2000 或更高版本)。 | |
Oemtilde | 美式标准键盘上的 OEM 波形符键(Windows 2000 或更高版本)。 | |
P | P 键。 | |
Pa1 | PA1 键。 | |
Packet | 用于将 Unicode 字符当作键击传递。Packet 键值是用于非键盘输入法的 32 位虚拟键值的低位字。 | |
PageDown | Page Down 键。 | |
PageUp | Page Up 键。 | |
Pause | Pause 键。 | |
Play | Play 键。 | |
Print 键。 | ||
PrintScreen | Print Screen 键。 | |
Prior | Page Up 键。 | |
ProcessKey | Process Key 键。 | |
Q | Q 键。 | |
R | R 键。 | |
RButton | 鼠标右按钮。 | |
RControlKey | 右 Ctrl 键。 | |
Return | Return 键。 | |
Right | 向右键。 | |
RMenu | 右 Alt 键。 | |
RShiftKey | 右 Shift 键。 | |
RWin | 右 Windows 徽标键(Microsoft Natural Keyboard,人体工程学键盘)。 | |
S | S 键。 | |
Scroll | Scroll Lock 键。 | |
Select | Select 键。 | |
SelectMedia | 选择媒体键(Windows 2000 或更高版本)。 | |
Separator | 分隔符键。 | |
Shift | Shift 修改键。 | |
ShiftKey | Shift 键。 | |
Sleep | 计算机睡眠键。 | |
Snapshot | Print Screen 键。 | |
Space | 空格键。 | |
Subtract | 减号键。 | |
T | T 键。 | |
Tab | Tab 键。 | |
U | U 键。 | |
Up | 向上键。 | |
V | V 键。 | |
VolumeDown | 减小音量键(Windows 2000 或更高版本)。 | |
VolumeMute | 静音键(Windows 2000 或更高版本)。 | |
VolumeUp | 增大音量键(Windows 2000 或更高版本)。 | |
W | W 键。 | |
X | X 键。 | |
XButton1 | 第一个 X 鼠标按钮(五个按钮的鼠标)。 | |
XButton2 | 第二个 X 鼠标按钮(五个按钮的鼠标)。 | |
Y | Y 键。 | |
Z | Z 键。 | |
Zoom | Zoom 键。 |
备注
Keys 类包含用于处理键盘输入的常数。键枚举的成员由一个键代码和一组修饰符构成(键代码和修饰符组成一个整数值)。在 Win32 应用程序编程接口 (API) 中,一个键值分为两部分:包含键代码(该代码与 Windows 虚拟键代码相同)的高序位和表示键修饰符(例如,Shift、Ctrl 和 Alt 键)的低序位。
提示
此枚举未提供方法来测试 Caps Lock 或 Num Lock 键当前是否已激活。可以使用下列技术之一来确定这些键是否已激活:
下表针对常规原始设备制造商 (OEM) 键以及更具体的美式键盘关联列出了由两个枚举的值表示的键代码值。
十六进制数值 |
美式键盘 |
常规 OEM |
---|---|---|
BA |
||
BF |
||
C0 |
||
DB |
||
DC |
||
DD |
||
DE |
||
E2 |
警告
.NET Framework 2.0 中添加了一个 IMEAccept 成员,可取代以前有拼写错误的 IMEAceept 项。为了向后兼容性而保留了旧版本,但在 .NET Framework 以后的版本中可能会将其删除。
示例
' Boolean flag used to determine when a character other than a number is entered.
Private nonNumberEntered As Boolean = False
' Handle the KeyDown event to determine the type of character entered into the control.
Private Sub textBox1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) _
Handles textBox1.KeyDown
' 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 OrElse e.KeyCode > Keys.D9 Then
' Determine whether the keystroke is a number from the keypad.
If e.KeyCode < Keys.NumPad0 OrElse e.KeyCode > Keys.NumPad9 Then
' Determine whether the keystroke is a backspace.
If e.KeyCode <> Keys.Back Then
' A non-numerical keystroke was pressed.
' Set the flag to true and evaluate in KeyPress event.
nonNumberEntered = True
End If
End If
End If
End Sub 'textBox1_KeyDown
' This event occurs after the KeyDown event and can be used
' to prevent characters from entering the control.
Private Sub textBox1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) _
Handles textBox1.KeyPress
' Check for the flag being set in the KeyDown event.
If nonNumberEntered = True Then
' Stop the character from being entered into the control since it is non-numerical.
e.Handled = True
End If
End Sub 'textBox1_KeyPress
End Class 'Form1
// 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;
}
}
}
}
// 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;
}
}
// Boolean flag used to determine when a character other than a number is entered.
private:
bool nonNumberEntered;
// Handle the KeyDown event to determine the type of character entered into the control.
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;
}
}
}
}
// This event occurs after the KeyDown event and can be used to prevent
// characters from entering the control.
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;
}
}
// Boolean flag used to determine when a character other than a number
// is entered.
private boolean 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.get_KeyCode().CompareTo(Keys.D0)) < 0
|| (e.get_KeyCode().CompareTo(Keys.D9) > 0)) {
// Determine whether the keystroke is a number from the keypad.
if ((e.get_KeyCode().CompareTo(Keys.NumPad0) < 0
|| (e.get_KeyCode().CompareTo(Keys.NumPad9)) > 0)) {
// Determine whether the keystroke is a backspace.
if (!(e.get_KeyCode().Equals(Keys.Back))) {
// A non-numerical keystroke was pressed.
// Set the flag to true and evaluate in KeyPress event.
nonNumberEntered = true;
}
}
}
} //textBox1_KeyDown
// 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.set_Handled(true);
}
} //textBox1_KeyPress
平台
Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition
.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。
版本信息
.NET Framework
受以下版本支持:2.0、1.1、1.0
.NET Compact Framework
受以下版本支持:2.0、1.0
请参见
参考
System.Windows.Forms 命名空间
KeysConverter
SendKeys
System.ConsoleKey
Shortcut
Control.ModifierKeys 属性
IsKeyLocked
KeyEventArgs 类