下面列出了javax.swing.KeyStroke#toString ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void setKeyBindings() {
int condition = javax.swing.JComponent.WHEN_IN_FOCUSED_WINDOW;
final javax.swing.InputMap inputMap = mapPanel.getInputMap(condition);
final javax.swing.ActionMap actionMap = mapPanel.getActionMap();
boolean[] keyPressed = { true, false };
//boolean[] keys = new boolean[KeyEvent.KEY_TYPED];
//keys[evt.getKeyCode()] = true;
for (Integer keyCode : keyboardMap.keySet()) {
KeyboardDirection dir = keyboardMap.get(keyCode);
for (boolean onKeyPress : keyPressed) {
boolean onKeyRelease = !onKeyPress;
KeyStroke keyStroke = KeyStroke.getKeyStroke(keyCode, 0, onKeyRelease);
Object key = keyStroke.toString();
inputMap.put(keyStroke, key);
actionMap.put(key, new KeyBindingsAction(dir, onKeyPress));
}
}
}
private void putActionDelegate(InputMap map, KeyStroke ks) {
String binding = COPY_ACTION_DELEGATE+ks.toString();
Action action = getCopyActionDelegate(map, ks);
if (action != null) {
getActionMap().put(binding, action);
map.put(ks, binding);
}
}
private static String getExpandKeyStrokeText(KeyStroke keyStroke) {
String expandKeyStrokeText;
if (keyStroke.equals(KeyStroke.getKeyStroke(' '))) { //NOI18N
expandKeyStrokeText = "SPACE"; // NOI18N
} else if (keyStroke.equals(KeyStroke.getKeyStroke(new Character(' '), InputEvent.SHIFT_MASK))) { //NOI18N
expandKeyStrokeText = "Shift-SPACE"; // NOI18N
} else if (keyStroke.equals(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0))) {
expandKeyStrokeText = "TAB"; // NOI18N
} else if (keyStroke.equals(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0))) {
expandKeyStrokeText = "ENTER"; // NOI18N
} else {
expandKeyStrokeText = keyStroke.toString();
}
return expandKeyStrokeText;
}
private void addSpaceActionhandler(JComponent host) {
InputMap inputMap = host.getInputMap();
ActionMap actionMap = host.getActionMap();
KeyStroke keyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0);
String key = keyStroke.toString();
inputMap.put(keyStroke, key);
actionMap.put(key, invertCheckAction);
}
private void setAccelerator(JButton button, KeyStroke ks) {
Command cmd = mMap.get(button);
cmd.setAccelerator(ks);
button.setText(getAcceleratorText(cmd));
button.invalidate();
Preferences prefs = Preferences.getInstance();
String key = cmd.getCommand();
String override = null;
if (!cmd.hasOriginalAccelerator()) {
override = ks != null ? ks.toString() : NONE;
}
prefs.setKeyBindingOverride(key, override);
}
/**
* Creates a {@code String} that keeps the attributes given
* {@code KeyStroke}. This {@code String} can be used to
* store the key stroke in an XML-file.
*
* @param keyStroke The {@code KeyStroke}.
* @return A {@code String} that produces a key stroke equal to the
* given {@code KeyStroke} if passed as a parameter to
* {@code getAWTKeyStroke(String)}.
*/
private static String getKeyStrokeText(KeyStroke keyStroke) {
return (keyStroke == null) ? "" : keyStroke.toString();
}