下面列出了java.awt.event.KeyEvent#VK_UNDEFINED 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
final public static int getExtendedKeyCodeForChar( int c ) {
int uc = Character.toUpperCase( c );
int lc = Character.toLowerCase( c );
if (regularKeyCodesMap.containsKey( c )) {
if(regularKeyCodesMap.containsKey(uc)) {
return regularKeyCodesMap.get( uc );
}
return regularKeyCodesMap.get( c );
}
uc += 0x01000000;
lc += 0x01000000;
if (extendedKeyCodesSet.contains( uc )) {
return uc;
}else if (extendedKeyCodesSet.contains( lc )) {
return lc;
}
return KeyEvent.VK_UNDEFINED;
}
private void checkKeycodeArgument(int keycode) {
// rather than build a big table or switch statement here, we'll
// just check that the key isn't VK_UNDEFINED and assume that the
// peer implementations will throw an exception for other bogus
// values e.g. -1, 999999
if (keycode == KeyEvent.VK_UNDEFINED) {
throw new IllegalArgumentException("Invalid key code");
}
}
protected Expression instantiate(Object oldInstance, Encoder out) {
AWTKeyStroke key = (AWTKeyStroke) oldInstance;
char ch = key.getKeyChar();
int code = key.getKeyCode();
int mask = key.getModifiers();
boolean onKeyRelease = key.isOnKeyRelease();
Object[] args = null;
if (ch == KeyEvent.CHAR_UNDEFINED) {
args = !onKeyRelease
? new Object[]{code, mask}
: new Object[]{code, mask, onKeyRelease};
} else if (code == KeyEvent.VK_UNDEFINED) {
if (!onKeyRelease) {
args = (mask == 0)
? new Object[]{ch}
: new Object[]{ch, mask};
} else if (mask == 0) {
args = new Object[]{ch, onKeyRelease};
}
}
if (args == null) {
throw new IllegalStateException("Unsupported KeyStroke: " + key);
}
Class<?> type = key.getClass();
String name = type.getName();
// get short name of the class
int index = name.lastIndexOf('.') + 1;
if (index > 0) {
name = name.substring(index);
}
return new Expression( key, type, "get" + name, args );
}
/**
* Returns the type of <code>KeyEvent</code> which corresponds to
* this <code>AWTKeyStroke</code>.
*
* @return <code>KeyEvent.KEY_PRESSED</code>,
* <code>KeyEvent.KEY_TYPED</code>,
* or <code>KeyEvent.KEY_RELEASED</code>
* @see java.awt.event.KeyEvent
*/
public final int getKeyEventType() {
if (keyCode == KeyEvent.VK_UNDEFINED) {
return KeyEvent.KEY_TYPED;
} else {
return (onKeyRelease)
? KeyEvent.KEY_RELEASED
: KeyEvent.KEY_PRESSED;
}
}
/**
* Returns key + modifiers pair.
*
* @param c Symbol code.
* @return an array of two elements: key code and modifiers mask - a
* combination of InputEvent MASK fields.
*/
public int[] getKeyAndModifiers(char c) {
CharKey key = chars.get(c);
if (key != null) {
return new int[]{key.key, key.modifiers};
} else {
return new int[]{KeyEvent.VK_UNDEFINED, 0};
}
}
/**
* Returns the type of <code>KeyEvent</code> which corresponds to
* this <code>AWTKeyStroke</code>.
*
* @return <code>KeyEvent.KEY_PRESSED</code>,
* <code>KeyEvent.KEY_TYPED</code>,
* or <code>KeyEvent.KEY_RELEASED</code>
* @see java.awt.event.KeyEvent
*/
public final int getKeyEventType() {
if (keyCode == KeyEvent.VK_UNDEFINED) {
return KeyEvent.KEY_TYPED;
} else {
return (onKeyRelease)
? KeyEvent.KEY_RELEASED
: KeyEvent.KEY_PRESSED;
}
}
/**
* Returns a string that displays and identifies this object's properties.
* The <code>String</code> returned by this method can be passed
* as a parameter to <code>getAWTKeyStroke(String)</code> to produce
* a key stroke equal to this key stroke.
*
* @return a String representation of this object
* @see #getAWTKeyStroke(String)
*/
public String toString() {
if (keyCode == KeyEvent.VK_UNDEFINED) {
return getModifiersText(modifiers) + "typed " + keyChar;
} else {
return getModifiersText(modifiers) +
(onKeyRelease ? "released" : "pressed") + " " +
getVKText(keyCode);
}
}
private static CharSequence getKeys(String keysFor, String sKeyStroke) {
KeyStroke ks = KeyStroke.getKeyStroke(sKeyStroke);
if (ks == null) {
throw new WebDriverException("Unable to parse keystroke for " + keysFor + " trying to parse " + sKeyStroke);
}
StringBuilder sb = new StringBuilder();
int modifiers = ks.getModifiers();
if ((modifiers & InputEvent.CTRL_DOWN_MASK) == InputEvent.CTRL_DOWN_MASK) {
sb.append(Keys.CONTROL);
}
if ((modifiers & InputEvent.ALT_DOWN_MASK) == InputEvent.ALT_DOWN_MASK) {
sb.append(Keys.ALT);
}
if ((modifiers & InputEvent.META_DOWN_MASK) == InputEvent.META_DOWN_MASK) {
sb.append(Keys.META);
}
if ((modifiers & InputEvent.SHIFT_DOWN_MASK) == InputEvent.SHIFT_DOWN_MASK) {
sb.append(Keys.SHIFT);
}
int keyCode = ks.getKeyCode();
if (keyCode == KeyEvent.VK_UNDEFINED) {
sb.append(ks.getKeyChar());
} else {
sb.append(keyCharFromKeyCode(keyCode, keysFor));
}
sb.append(Keys.NULL);
return sb.toString();
}
private void checkKeycodeArgument(int keycode) {
// rather than build a big table or switch statement here, we'll
// just check that the key isn't VK_UNDEFINED and assume that the
// peer implementations will throw an exception for other bogus
// values e.g. -1, 999999
if (keycode == KeyEvent.VK_UNDEFINED) {
throw new IllegalArgumentException("Invalid key code");
}
}
private void checkKeycodeArgument(int keycode) {
// rather than build a big table or switch statement here, we'll
// just check that the key isn't VK_UNDEFINED and assume that the
// peer implementations will throw an exception for other bogus
// values e.g. -1, 999999
if (keycode == KeyEvent.VK_UNDEFINED) {
throw new IllegalArgumentException("Invalid key code");
}
}
protected Expression instantiate(Object oldInstance, Encoder out) {
AWTKeyStroke key = (AWTKeyStroke) oldInstance;
char ch = key.getKeyChar();
int code = key.getKeyCode();
int mask = key.getModifiers();
boolean onKeyRelease = key.isOnKeyRelease();
Object[] args = null;
if (ch == KeyEvent.CHAR_UNDEFINED) {
args = !onKeyRelease
? new Object[]{code, mask}
: new Object[]{code, mask, onKeyRelease};
} else if (code == KeyEvent.VK_UNDEFINED) {
if (!onKeyRelease) {
args = (mask == 0)
? new Object[]{ch}
: new Object[]{ch, mask};
} else if (mask == 0) {
args = new Object[]{ch, onKeyRelease};
}
}
if (args == null) {
throw new IllegalStateException("Unsupported KeyStroke: " + key);
}
Class<?> type = key.getClass();
String name = type.getName();
// get short name of the class
int index = name.lastIndexOf('.') + 1;
if (index > 0) {
name = name.substring(index);
}
return new Expression( key, type, "get" + name, args );
}
private AWTKeyStroke getInputMethodSelectionKeyStroke(Preferences root) {
try {
if (root.nodeExists(inputMethodSelectionKeyPath)) {
Preferences node = root.node(inputMethodSelectionKeyPath);
int keyCode = node.getInt(inputMethodSelectionKeyCodeName, KeyEvent.VK_UNDEFINED);
if (keyCode != KeyEvent.VK_UNDEFINED) {
int modifiers = node.getInt(inputMethodSelectionKeyModifiersName, 0);
return AWTKeyStroke.getAWTKeyStroke(keyCode, modifiers);
}
}
} catch (BackingStoreException bse) {
}
return null;
}
private void checkKeycodeArgument(int keycode) {
// rather than build a big table or switch statement here, we'll
// just check that the key isn't VK_UNDEFINED and assume that the
// peer implementations will throw an exception for other bogus
// values e.g. -1, 999999
if (keycode == KeyEvent.VK_UNDEFINED) {
throw new IllegalArgumentException("Invalid key code");
}
}
private void checkKeycodeArgument(int keycode) {
// rather than build a big table or switch statement here, we'll
// just check that the key isn't VK_UNDEFINED and assume that the
// peer implementations will throw an exception for other bogus
// values e.g. -1, 999999
if (keycode == KeyEvent.VK_UNDEFINED) {
throw new IllegalArgumentException("Invalid key code");
}
}
/**
* Returns the type of <code>KeyEvent</code> which corresponds to
* this <code>AWTKeyStroke</code>.
*
* @return <code>KeyEvent.KEY_PRESSED</code>,
* <code>KeyEvent.KEY_TYPED</code>,
* or <code>KeyEvent.KEY_RELEASED</code>
* @see java.awt.event.KeyEvent
*/
public final int getKeyEventType() {
if (keyCode == KeyEvent.VK_UNDEFINED) {
return KeyEvent.KEY_TYPED;
} else {
return (onKeyRelease)
? KeyEvent.KEY_RELEASED
: KeyEvent.KEY_PRESSED;
}
}
/**
* Returns a string that displays and identifies this object's properties.
* The <code>String</code> returned by this method can be passed
* as a parameter to <code>getAWTKeyStroke(String)</code> to produce
* a key stroke equal to this key stroke.
*
* @return a String representation of this object
* @see #getAWTKeyStroke(String)
*/
public String toString() {
if (keyCode == KeyEvent.VK_UNDEFINED) {
return getModifiersText(modifiers) + "typed " + keyChar;
} else {
return getModifiersText(modifiers) +
(onKeyRelease ? "released" : "pressed") + " " +
getVKText(keyCode);
}
}
/**
* Returns a string that displays and identifies this object's properties.
* The <code>String</code> returned by this method can be passed
* as a parameter to <code>getAWTKeyStroke(String)</code> to produce
* a key stroke equal to this key stroke.
*
* @return a String representation of this object
* @see #getAWTKeyStroke(String)
*/
public String toString() {
if (keyCode == KeyEvent.VK_UNDEFINED) {
return getModifiersText(modifiers) + "typed " + keyChar;
} else {
return getModifiersText(modifiers) +
(onKeyRelease ? "released" : "pressed") + " " +
getVKText(keyCode);
}
}
@Override
@SuppressWarnings("Duplicates")
protected synchronized void doHandleKeyPress(KeyEvent e) {
if (handler == null) return;
//LOGGER.debug("key pressed: " + e.paramString());
// Get code of the pressed key.
// Keypad arrows are translated to regular arrow keys.
final int keyCode;
switch (e.getKeyCode()) {
case KeyEvent.VK_KP_DOWN:
keyCode = KeyEvent.VK_DOWN;
break;
case KeyEvent.VK_KP_LEFT:
keyCode = KeyEvent.VK_LEFT;
break;
case KeyEvent.VK_KP_RIGHT:
keyCode = KeyEvent.VK_RIGHT;
break;
case KeyEvent.VK_KP_UP:
keyCode = KeyEvent.VK_UP;
break;
default:
keyCode = e.getKeyCode();
break;
}
// Never press undefined key codes.
if (keyCode == KeyEvent.VK_UNDEFINED) {
return;
}
// Never send caps lock, num lock and scroll lock key.
if (keyCode == KeyEvent.VK_CAPS_LOCK || keyCode == KeyEvent.VK_NUM_LOCK || keyCode == KeyEvent.VK_SCROLL_LOCK) {
return;
}
// Detect, if a control key was pressed.
final boolean isControlKey = e.isActionKey() ||
keyCode == KeyEvent.VK_BACK_SPACE ||
keyCode == KeyEvent.VK_DELETE ||
keyCode == KeyEvent.VK_ENTER ||
keyCode == KeyEvent.VK_SPACE ||
keyCode == KeyEvent.VK_TAB ||
keyCode == KeyEvent.VK_ESCAPE ||
keyCode == KeyEvent.VK_ALT ||
keyCode == KeyEvent.VK_ALT_GRAPH ||
keyCode == KeyEvent.VK_CONTROL ||
keyCode == KeyEvent.VK_SHIFT ||
keyCode == KeyEvent.VK_META;
// Press control keys.
if (isControlKey) {
//LOGGER.debug("press key \"{}\" ({})", keyCode, KeyEvent.getKeyText(keyCode));
handler.sendKeyPress(keyCode);
e.consume();
}
// Press other keys, if they are pressed together with a modifier key.
else if (e.isControlDown() || e.isMetaDown() || windowsKeyDown || (!SystemUtils.IS_OS_MAC && e.isAltDown())) {
//LOGGER.debug("press key \"{}\" ({})", keyCode, KeyEvent.getKeyText(keyCode));
handler.sendKeyPress(keyCode);
if (!pressedKeys.contains(keyCode))
pressedKeys.add(keyCode);
e.consume();
}
// Remember, that the Windows key was pressed.
if (keyCode == KeyEvent.VK_WINDOWS) {
synchronized (Frame.this) {
windowsKeyDown = true;
}
}
}
/**
* Dispatches committed text to a client component.
* Called by composition window.
*
* @param client The component that the text should get dispatched to.
* @param text The iterator providing access to the committed
* (and possible composed) text.
* @param committedCharacterCount The number of committed characters in the text.
*/
synchronized void dispatchCommittedText(Component client,
AttributedCharacterIterator text,
int committedCharacterCount) {
// note that the client is not always the current client component -
// some host input method adapters may dispatch input method events
// through the Java event queue, and we may have switched clients while
// the event was in the queue.
if (committedCharacterCount == 0
|| text.getEndIndex() <= text.getBeginIndex()) {
return;
}
long time = System.currentTimeMillis();
dispatchingCommittedText = true;
try {
InputMethodRequests req = client.getInputMethodRequests();
if (req != null) {
// active client -> send text as InputMethodEvent
int beginIndex = text.getBeginIndex();
AttributedCharacterIterator toBeCommitted =
(new AttributedString(text, beginIndex, beginIndex + committedCharacterCount)).getIterator();
InputMethodEvent inputEvent = new InputMethodEvent(
client,
InputMethodEvent.INPUT_METHOD_TEXT_CHANGED,
toBeCommitted,
committedCharacterCount,
null, null);
client.dispatchEvent(inputEvent);
} else {
// passive client -> send text as KeyEvents
char keyChar = text.first();
while (committedCharacterCount-- > 0 && keyChar != CharacterIterator.DONE) {
KeyEvent keyEvent = new KeyEvent(client, KeyEvent.KEY_TYPED,
time, 0, KeyEvent.VK_UNDEFINED, keyChar);
client.dispatchEvent(keyEvent);
keyChar = text.next();
}
}
} finally {
dispatchingCommittedText = false;
}
}
/**
* Returns an instance of a KeyStroke, specifying whether the key is
* considered to be activated when it is pressed or released. Unlike all
* other factory methods in this class, the instances returned by this
* method are not necessarily cached or shared.
*
* @param keyChar the character value for a keyboard key
* @param onKeyRelease <code>true</code> if this KeyStroke corresponds to a
* key release; <code>false</code> otherwise.
* @return a KeyStroke object for that key
* @deprecated use getKeyStroke(char)
*/
@Deprecated
public static KeyStroke getKeyStroke(char keyChar, boolean onKeyRelease) {
return new KeyStroke(keyChar, KeyEvent.VK_UNDEFINED, 0, onKeyRelease);
}