|
JavaTM 2 Platform Standard Ed. 6 |
|||||||||
| 上一个类 下一个类 | 框架 无框架 | |||||||||
| 摘要: 嵌套 | 字段 | 构造方法 | 方法 | 详细信息: 字段 | 构造方法 | 方法 | |||||||||
java.lang.Objectjava.util.EventObject
java.awt.AWTEvent
java.awt.event.ComponentEvent
java.awt.event.InputEvent
java.awt.event.KeyEvent
public class KeyEvent
表示组件中发生键击的事件。
当按下、释放或键入某个键时,组件对象(如文本字段)将生成此低级别事件。该事件被传递给每一个 KeyListener 或 KeyAdapter 对象,这些对象使用组件的 addKeyListener 方法注册,以接收此类事件。(KeyAdapter 对象实现 KeyListener 接口。)发生事件时,所有此类侦听器对象都将获得此 KeyEvent。
“键入键”事件 是高级别事件,通常不依赖于平台或键盘布局。输入 Unicode 字符时生成此类事件,它们被认为是发现字符输入的最佳方式。最简单的情况是,按下单个键(如 "a")将产生键入键事件。但是,字符经常是通过一系列按键(如‘shift’+‘a’)产生的,按下键事件和键入键事件的映射关系可能是多对一或多对多的。键释放通常不需要生成键入键事件,但在某些情况下,只有释放了某个键后才能生成键入键事件(如在 Windows 中通过 Alt-Numpad 方法来输入 ASCII 序列)。对于不生成 Unicode 字符的键是不会生成键入键事件的(如动作键、修改键等等)。
getKeyChar 方法总是返回有效的 Unicode 字符或 CHAR_UNDEFINED。KEY_TYPED 事件报告字符输入:KEY_PRESSED 和 KEY_RELEASED 事件不必与字符输入关联。因此,可以保证 getKeyChar 方法的结果只对 KEY_TYPED 事件有意义。
对于按下键和释放键事件,getKeyCode 方法返回该事件的 keyCode。对于键入键事件,getKeyCode 方法总是返回 VK_UNDEFINED。
“按下键”和“释放键”事件 是低级别事件,依赖于平台和键盘布局。只要按下或释放键就生成这些事件,它们是发现不生成字符输入的键(如动作键、修改键等等)的惟一方式。通过 getKeyCode 方法可指出按下或释放的键,该方法返回一个虚拟键码。
虚拟键码 用于报告按下了键盘上的哪个键,而不是一次或多次键击组合生成的字符(如 "A" 是由 shift + "a" 生成的)。
例如,按下 Shift 键会生成 keyCode 为 VK_SHIFT 的 KEY_PRESSED 事件,而按下 'a' 键将生成 keyCode 为 VK_A 的 KEY_PRESSED 事件。释放 'a' 键后,会激发 keyCode 为 VK_A 的 KEY_RELEASED 事件。另外,还会生成一个 keyChar 值为 'A' 的 KEY_TYPED 事件。
按下和释放键盘上的键会导致(依次)生成以下键事件:
KEY_PRESSED
KEY_TYPED(只在可生成有效 Unicode 字符时产生。)
KEY_RELEASED
但在某些情况下(例如,在激活自动重复或输入方法时),该顺序可能会有所不同(并且与平台有关)。
注:
警告:除了 Java 语言定义的这些键之外(VK_ENTER、VK_BACK_SPACE 和 VK_TAB),不要依赖 VK_ 常量值。Sun 保留将来根据需要更改这些值的权利,以适应更大范围的键盘。
KeyAdapter,
KeyListener,
Tutorial: Writing a Key Listener,
序列化表格| 字段摘要 | |
|---|---|
static char |
CHAR_UNDEFINED
KEY_PRESSED 和 KEY_RELEASED 事件没有映射到一个有效的 Unicode 字符时使用此值作为 keyChar 值。 |
static int |
KEY_FIRST
用于键事件的 id 范围的起始编号。 |
static int |
KEY_LAST
用于键事件的 id 范围的结束编号。 |
static int |
KEY_LOCATION_LEFT
该常量指示按下或释放的键位于左侧(此键的位置有多种可能性)。 |
static int |
KEY_LOCATION_NUMPAD
该常量指示按键事件发自于数字键盘或对应于数字键盘的虚拟键。 |
static int |
KEY_LOCATION_RIGHT
该常量指示按下或释放的键位于右侧(此键的位置有多种可能性)。 |
static int |
KEY_LOCATION_STANDARD
该常量指示按下或释放的键不区分左右,并且不是发自数字键盘(或者不是发自对应于数字键盘的虚拟键)。 |
static int |
KEY_LOCATION_UNKNOWN
指示 keyLocation 为不确定或不相关的常量。 |
static int |
KEY_PRESSED
“按下键”事件。 |
static int |
KEY_RELEASED
“释放键”事件。 |
static int |
KEY_TYPED
“键入键”事件。 |
static int |
VK_0
VK_0 到 VK_9 与 ASCII 的‘0’到‘9’(0x30 - 0x39) 相同 |
static int |
VK_1
|
static int |
VK_2
|
static int |
VK_3
|
static int |
VK_4
|
static int |
VK_5
|
static int |
VK_6
|
static int |
VK_7
|
static int |
VK_8
|
static int |
VK_9
|
static int |
VK_A
VK_A 到 VK_Z 与 ASCII 的‘A’到‘Z’(0x41 - 0x5A) 相同 |
static int |
VK_ACCEPT
用于 Accept 或 Commit 功能键的常量。 |
static int |
VK_ADD
|
static int |
VK_AGAIN
|
static int |
VK_ALL_CANDIDATES
用于 All Candidates 功能键的常量。 |
static int |
VK_ALPHANUMERIC
用于 Alphanumeric 功能键的常量。 |
static int |
VK_ALT
|
static int |
VK_ALT_GRAPH
用于 AltGraph 功能键的常量。 |
static int |
VK_AMPERSAND
|
static int |
VK_ASTERISK
|
static int |
VK_AT
用于 "@" 键的常量。 |
static int |
VK_B
|
static int |
VK_BACK_QUOTE
|
static int |
VK_BACK_SLASH
用于反斜杠键 "\" 的常量。 |
static int |
VK_BACK_SPACE
|
static int |
VK_BEGIN
用于 Begin 键的常量。 |
static int |
VK_BRACELEFT
|
static int |
VK_BRACERIGHT
|
static int |
VK_C
|
static int |
VK_CANCEL
|
static int |
VK_CAPS_LOCK
|
static int |
VK_CIRCUMFLEX
用于 "^" 键的常量。 |
static int |
VK_CLEAR
|
static int |
VK_CLOSE_BRACKET
用于右方括号键 "]" 的常量。 |
static int |
VK_CODE_INPUT
用于 Code Input 功能键的常量。 |
static int |
VK_COLON
用于 ":" 键的常量。 |
static int |
VK_COMMA
用于逗号键 "," 的常量。 |
static int |
VK_COMPOSE
用于 Compose 功能键的常量。 |
static int |
VK_CONTEXT_MENU
用于 Microsoft Windows 上下文菜单键的常量。 |
static int |
VK_CONTROL
|
static int |
VK_CONVERT
用于 Convert 功能键的常量。 |
static int |
VK_COPY
|
static int |
VK_CUT
|
static int |
VK_D
|
static int |
VK_DEAD_ABOVEDOT
|
static int |
VK_DEAD_ABOVERING
|
static int |
VK_DEAD_ACUTE
|
static int |
VK_DEAD_BREVE
|
static int |
VK_DEAD_CARON
|
static int |
VK_DEAD_CEDILLA
|
static int |
VK_DEAD_CIRCUMFLEX
|
static int |
VK_DEAD_DIAERESIS
|
static int |
VK_DEAD_DOUBLEACUTE
|
static int |
VK_DEAD_GRAVE
|
static int |
VK_DEAD_IOTA
|
static int |
VK_DEAD_MACRON
|
static int |
VK_DEAD_OGONEK
|
static int |
VK_DEAD_SEMIVOICED_SOUND
|
static int |
VK_DEAD_TILDE
|
static int |
VK_DEAD_VOICED_SOUND
|
static int |
VK_DECIMAL
|
static int |
VK_DELETE
|
static int |
VK_DIVIDE
|
static int |
VK_DOLLAR
用于 "$" 键的常量。 |
static int |
VK_DOWN
用于非数字键盘向下方向键的常量。 |
static int |
VK_E
|
static int |
VK_END
|
static int |
VK_ENTER
|
static int |
VK_EQUALS
用于等号键 "=" 的常量。 |
static int |
VK_ESCAPE
|
static int |
VK_EURO_SIGN
用于欧洲货币符号键的常量。 |
static int |
VK_EXCLAMATION_MARK
用于 "!" 键的常量。 |
static int |
VK_F
|
static int |
VK_F1
用于 F1 功能键的常量。 |
static int |
VK_F10
用于 F10 功能键的常量。 |
static int |
VK_F11
用于 F11 功能键的常量。 |
static int |
VK_F12
用于 F12 功能键的常量。 |
static int |
VK_F13
用于 F13 功能键的常量。 |
static int |
VK_F14
用于 F14 功能键的常量。 |
static int |
VK_F15
用于 F15 功能键的常量。 |
static int |
VK_F16
用于 F16 功能键的常量。 |
static int |
VK_F17
用于 F17 功能键的常量。 |
static int |
VK_F18
用于 F18 功能键的常量。 |
static int |
VK_F19
用于 F19 功能键的常量。 |
static int |
VK_F2
用于 F2 功能键的常量。 |
static int |
VK_F20
用于 F20 功能键的常量。 |
static int |
VK_F21
用于 F21 功能键的常量。 |
static int |
VK_F22
用于 F22 功能键的常量。 |
static int |
VK_F23
用于 F23 功能键的常量。 |
static int |
VK_F24
用于 F24 功能键的常量。 |
static int |
VK_F3
用于 F3 功能键的常量。 |
static int |
VK_F4
用于 F4 功能键的常量。 |
static int |
VK_F5
用于 F5 功能键的常量。 |
static int |
VK_F6
用于 F6 功能键的常量。 |
static int |
VK_F7
用于 F7 功能键的常量。 |
static int |
VK_F8
用于 F8 功能键的常量。 |
static int |
VK_F9
用于 F9 功能键的常量。 |
static int |
VK_FINAL
|
static int |
VK_FIND
|
static int |
VK_FULL_WIDTH
用于 Full-Width Characters 功能键的常量。 |
static int |
VK_G
|
static int |
VK_GREATER
|
static int |
VK_H
|
static int |
VK_HALF_WIDTH
用于 Half-Width Characters 功能键的常量。 |
static int |
VK_HELP
|
static int |
VK_HIRAGANA
用于 Hiragana 功能键的常量。 |
static int |
VK_HOME
|
static int |
VK_I
|
static int |
VK_INPUT_METHOD_ON_OFF
用于输入法开/关键的常量。 |
static int |
VK_INSERT
|
static int |
VK_INVERTED_EXCLAMATION_MARK
用于反向感叹号键的常量。 |
static int |
VK_J
|
static int |
VK_JAPANESE_HIRAGANA
用于 Japanese-Hiragana 功能键的常量。 |
static int |
VK_JAPANESE_KATAKANA
用于 Japanese-Katakana 功能键的常量。 |
static int |
VK_JAPANESE_ROMAN
用于 Japanese-Roman 功能键的常量。 |
static int |
VK_K
|
static int |
VK_KANA
|
static int |
VK_KANA_LOCK
用于锁定 Kana 功能键的常量。 |
static int |
VK_KANJI
|
static int |
VK_KATAKANA
用于 Katakana 功能键的常量。 |
static int |
VK_KP_DOWN
用于数字键盘向下方向键的常量。 |
static int |
VK_KP_LEFT
用于数字键盘向左方向键的常量。 |
static int |
VK_KP_RIGHT
用于数字键盘向右方向键的常量。 |
static int |
VK_KP_UP
用于数字键盘向上方向键的常量。 |
static int |
VK_L
|
static int |
VK_LEFT
用于非数字键盘向左方向键的常量。 |
static int |
VK_LEFT_PARENTHESIS
用于 "(" 键的常量。 |
static int |
VK_LESS
|
static int |
VK_M
|
static int |
VK_META
|
static int |
VK_MINUS
用于减号键 "-" 的常量 |
static int |
VK_MODECHANGE
|
static int |
VK_MULTIPLY
|
static int |
VK_N
|
static int |
VK_NONCONVERT
用于 Don't Convert 功能键的常量。 |
static int |
VK_NUM_LOCK
|
static int |
VK_NUMBER_SIGN
用于 "#" 键的常量。 |
static int |
VK_NUMPAD0
|
static int |
VK_NUMPAD1
|
static int |
VK_NUMPAD2
|
static int |
VK_NUMPAD3
|
static int |
VK_NUMPAD4
|
static int |
VK_NUMPAD5
|
static int |
VK_NUMPAD6
|
static int |
VK_NUMPAD7
|
static int |
VK_NUMPAD8
|
static int |
VK_NUMPAD9
|
static int |
VK_O
|
static int |
VK_OPEN_BRACKET
用于左方括号键 "[" 的常量。 |
static int |
VK_P
|
static int |
VK_PAGE_DOWN
|
static int |
VK_PAGE_UP
|
static int |
VK_PASTE
|
static int |
VK_PAUSE
|
static int |
VK_PERIOD
用于句点键 "." 的常量。 |
static int |
VK_PLUS
用于 "+" 键的常量。 |
static int |
VK_PREVIOUS_CANDIDATE
用于 Previous Candidate 功能键的常量。 |
static int |
VK_PRINTSCREEN
|
static int |
VK_PROPS
|
static int |
VK_Q
|
static int |
VK_QUOTE
|
static int |
VK_QUOTEDBL
|
static int |
VK_R
|
static int |
VK_RIGHT
用于非数字键盘向右方向键的常量。 |
static int |
VK_RIGHT_PARENTHESIS
用于 ")" 键的常量。 |
static int |
VK_ROMAN_CHARACTERS
用于 Roman Characters 功能键的常量。 |
static int |
VK_S
|
static int |
VK_SCROLL_LOCK
|
static int |
VK_SEMICOLON
用于分号键 ";" 的常量。 |
static int |
VK_SEPARATER
此常量已废弃,包括它仅为了向后兼容。 |
static int |
VK_SEPARATOR
用于数字键盘分隔符键的常量。 |
static int |
VK_SHIFT
|
static int |
VK_SLASH
用于正斜杠键 "/" 的常量。 |
static int |
VK_SPACE
|
static int |
VK_STOP
|
static int |
VK_SUBTRACT
|
static int |
VK_T
|
static int |
VK_TAB
|
static int |
VK_U
|
static int |
VK_UNDEFINED
此值用于指出 keyCode 是未知的。 |
static int |
VK_UNDERSCORE
用于 "_" 键的常量。 |
static int |
VK_UNDO
|
static int |
VK_UP
用于非数字键盘向上方向键的常量。 |
static int |
VK_V
|
static int |
VK_W
|
static int |
VK_WINDOWS
用于 Microsoft Windows 的 "Windows" 键的常量。 |
static int |
VK_X
|
static int |
VK_Y
|
static int |
VK_Z
|
| 从类 java.awt.event.ComponentEvent 继承的字段 |
|---|
COMPONENT_FIRST, COMPONENT_HIDDEN, COMPONENT_LAST, COMPONENT_MOVED, COMPONENT_RESIZED, COMPONENT_SHOWN |
| 从类 java.util.EventObject 继承的字段 |
|---|
source |
| 构造方法摘要 | |
|---|---|
KeyEvent(Component source,
int id,
long when,
int modifiers,
int keyCode)
已过时。 从 JDK1.1 开始 |
|
KeyEvent(Component source,
int id,
long when,
int modifiers,
int keyCode,
char keyChar)
构造一个 KeyEvent 对象。 |
|
KeyEvent(Component source,
int id,
long when,
int modifiers,
int keyCode,
char keyChar,
int keyLocation)
|
|
| 方法摘要 | |
|---|---|
char |
getKeyChar()
返回与此事件中的键关联的字符。 |
int |
getKeyCode()
返回与此事件中的键关联的整数 keyCode。 |
int |
getKeyLocation()
返回产生此按键事件的键位置。 |
static String |
getKeyModifiersText(int modifiers)
返回描述修改键的 String,如 "Shift" 或 "Ctrl+Shift"。 |
static String |
getKeyText(int keyCode)
返回描述 keyCode 的 String,如 "HOME"、"F1" 或 "A"。 |
boolean |
isActionKey()
返回此事件中的键是否为“动作”键。 |
String |
paramString()
返回标识此事件的参数字符串。 |
void |
setKeyChar(char keyChar)
设置 keyCode 值,以表示某个逻辑字符。 |
void |
setKeyCode(int keyCode)
设置 keyCode 值,以表示某个物理键。 |
void |
setModifiers(int modifiers)
已过时。 从 JDK1.1.4 开始 |
| 从类 java.awt.event.InputEvent 继承的方法 |
|---|
consume, getModifiers, getModifiersEx, getModifiersExText, getWhen, isAltDown, isAltGraphDown, isConsumed, isControlDown, isMetaDown, isShiftDown |
| 从类 java.awt.event.ComponentEvent 继承的方法 |
|---|
getComponent |
| 从类 java.awt.AWTEvent 继承的方法 |
|---|
getID, setSource, toString |
| 从类 java.util.EventObject 继承的方法 |
|---|
getSource |
| 从类 java.lang.Object 继承的方法 |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| 字段详细信息 |
|---|
public static final int KEY_FIRST
public static final int KEY_LAST
public static final int KEY_TYPED
public static final int KEY_PRESSED
public static final int KEY_RELEASED
public static final int VK_ENTER
public static final int VK_BACK_SPACE
public static final int VK_TAB
public static final int VK_CANCEL
public static final int VK_CLEAR
public static final int VK_SHIFT
public static final int VK_CONTROL
public static final int VK_ALT
public static final int VK_PAUSE
public static final int VK_CAPS_LOCK
public static final int VK_ESCAPE
public static final int VK_SPACE
public static final int VK_PAGE_UP
public static final int VK_PAGE_DOWN
public static final int VK_END
public static final int VK_HOME
public static final int VK_LEFT
VK_KP_LEFT,
常量字段值public static final int VK_UP
VK_KP_UP,
常量字段值public static final int VK_RIGHT
VK_KP_RIGHT,
常量字段值public static final int VK_DOWN
VK_KP_DOWN,
常量字段值public static final int VK_COMMA
public static final int VK_MINUS
public static final int VK_PERIOD
public static final int VK_SLASH
public static final int VK_0
public static final int VK_1
public static final int VK_2
public static final int VK_3
public static final int VK_4
public static final int VK_5
public static final int VK_6
public static final int VK_7
public static final int VK_8
public static final int VK_9
public static final int VK_SEMICOLON
public static final int VK_EQUALS
public static final int VK_A
public static final int VK_B
public static final int VK_C
public static final int VK_D
public static final int VK_E
public static final int VK_F
public static final int VK_G
public static final int VK_H
public static final int VK_I
public static final int VK_J
public static final int VK_K
public static final int VK_L
public static final int VK_M
public static final int VK_N
public static final int VK_O
public static final int VK_P
public static final int VK_Q
public static final int VK_R
public static final int VK_S
public static final int VK_T
public static final int VK_U
public static final int VK_V
public static final int VK_W
public static final int VK_X
public static final int VK_Y
public static final int VK_Z
public static final int VK_OPEN_BRACKET
public static final int VK_BACK_SLASH
public static final int VK_CLOSE_BRACKET
public static final int VK_NUMPAD0
public static final int VK_NUMPAD1
public static final int VK_NUMPAD2
public static final int VK_NUMPAD3
public static final int VK_NUMPAD4
public static final int VK_NUMPAD5
public static final int VK_NUMPAD6
public static final int VK_NUMPAD7
public static final int VK_NUMPAD8
public static final int VK_NUMPAD9
public static final int VK_MULTIPLY
public static final int VK_ADD
public static final int VK_SEPARATER
VK_SEPARATOR,
常量字段值public static final int VK_SEPARATOR
public static final int VK_SUBTRACT
public static final int VK_DECIMAL
public static final int VK_DIVIDE
public static final int VK_DELETE
public static final int VK_NUM_LOCK
public static final int VK_SCROLL_LOCK
public static final int VK_F1
public static final int VK_F2
public static final int VK_F3
public static final int VK_F4
public static final int VK_F5
public static final int VK_F6
public static final int VK_F7
public static final int VK_F8
public static final int VK_F9
public static final int VK_F10
public static final int VK_F11
public static final int VK_F12
public static final int VK_F13
public static final int VK_F14
public static final int VK_F15
public static final int VK_F16
public static final int VK_F17
public static final int VK_F18
public static final int VK_F19
public static final int VK_F20
public static final int VK_F21
public static final int VK_F22
public static final int VK_F23
public static final int VK_F24
public static final int VK_PRINTSCREEN
public static final int VK_INSERT
public static final int VK_HELP
public static final int VK_META
public static final int VK_BACK_QUOTE
public static final int VK_QUOTE
public static final int VK_KP_UP
VK_UP,
常量字段值public static final int VK_KP_DOWN
VK_DOWN,
常量字段值public static final int VK_KP_LEFT
VK_LEFT,
常量字段值public static final int VK_KP_RIGHT
VK_RIGHT,
常量字段值public static final int VK_DEAD_GRAVE
public static final int VK_DEAD_ACUTE
public static final int VK_DEAD_CIRCUMFLEX
public static final int VK_DEAD_TILDE
public static final int VK_DEAD_MACRON
public static final int VK_DEAD_BREVE
public static final int VK_DEAD_ABOVEDOT
public static final int VK_DEAD_DIAERESIS
public static final int VK_DEAD_ABOVERING
public static final int VK_DEAD_DOUBLEACUTE
public static final int VK_DEAD_CARON
public static final int VK_DEAD_CEDILLA
public static final int VK_DEAD_OGONEK
public static final int VK_DEAD_IOTA
public static final int VK_DEAD_VOICED_SOUND