// --------> // original code: // ftp://ftp.oreilly.de/pub/examples/english_examples/jswing2/code/goodies/Mapper.java // modified by terai // private Hashtable<Object, ArrayList<KeyStroke>> buildReverseMap(InputMap im) { // Hashtable<Object, ArrayList<KeyStroke>> h = new Hashtable<>(); // if (Objects.isNull(im.allKeys())) { // return h; // } // for (KeyStroke ks: im.allKeys()) { // Object name = im.get(ks); // if (h.containsKey(name)) { // h.get(name).add(ks); // } else { // ArrayList<KeyStroke> keylist = new ArrayList<>(); // keylist.add(ks); // h.put(name, keylist); // } // } // return h; // } private void loadBindingMap(Integer focusType, InputMap im, ActionMap am) { if (Objects.isNull(im.allKeys())) { return; } ActionMap tmpAm = new ActionMap(); for (Object actionMapKey : am.allKeys()) { tmpAm.put(actionMapKey, am.get(actionMapKey)); } for (KeyStroke ks : im.allKeys()) { Object actionMapKey = im.get(ks); Action action = am.get(actionMapKey); if (Objects.isNull(action)) { model.addBinding(new Binding(focusType, "____" + actionMapKey.toString(), ks.toString())); } else { model.addBinding(new Binding(focusType, actionMapKey.toString(), ks.toString())); } tmpAm.remove(actionMapKey); } if (Objects.isNull(tmpAm.allKeys())) { return; } for (Object actionMapKey : tmpAm.allKeys()) { model.addBinding(new Binding(focusType, actionMapKey.toString(), "")); } }
public static void sysoutInputMap(JComponent com) { InputMap im; KeyStroke[] keys; im = com.getInputMap(JComponent.WHEN_FOCUSED); keys = im.allKeys(); System.out.println("WHEN_FOCUSED"); if (keys != null) { for (KeyStroke ks : keys) { System.out.println(ks.toString() + "\t" + im.get(ks)); } } im = com.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); keys = im.allKeys(); System.out.println("WHEN_ANCESTOR_OF_FOCUSED_COMPONENT"); if (keys != null) { for (KeyStroke ks : keys) { System.out.println(ks.toString() + "\t" + im.get(ks)); } } im = com.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); keys = im.allKeys(); System.out.println("WHEN_IN_FOCUSED_WINDOW"); if (keys != null) { for (KeyStroke ks : keys) { System.out.println(ks.toString() + "\t" + im.get(ks)); } } }
// TODO accelerators for buttons private static void installAccelerator(Action action, final JButton button) { if (action.getValue(Action.ACCELERATOR_KEY) instanceof KeyStroke) { KeyStroke keyStroke = (KeyStroke) action.getValue(Action.ACCELERATOR_KEY); InputMap windowInputMap = SwingUtilities.getUIInputMap(button, JComponent.WHEN_IN_FOCUSED_WINDOW); if (windowInputMap == null) { windowInputMap = new ComponentInputMapUIResource(button); SwingUtilities.replaceUIInputMap(button, JComponent.WHEN_IN_FOCUSED_WINDOW, windowInputMap); } windowInputMap.put(keyStroke, keyStroke.toString()); button.getActionMap().put(keyStroke.toString(), action); } }
@NotNull public static String toKeyNotation(@NotNull KeyStroke key) { final char c = key.getKeyChar(); final int keyCode = key.getKeyCode(); final int modifiers = key.getModifiers(); if (c != CHAR_UNDEFINED && !isControlCharacter(c)) { return String.valueOf(c); } String prefix = ""; if ((modifiers & META_MASK) != 0) { prefix += "M-"; } if ((modifiers & ALT_MASK) != 0) { prefix += "A-"; } if ((modifiers & CTRL_MASK) != 0) { prefix += "C-"; } if ((modifiers & SHIFT_MASK) != 0) { prefix += "S-"; } String name = VIM_KEY_VALUES.get(keyCode); if (name != null) { if (UPPERCASE_DISPLAY_KEY_NAMES.contains(name)) { name = name.toUpperCase(); } else { name = StringUtil.capitalize(name); } } if (name == null) { final String escape = toEscapeNotation(key); if (escape != null) { return escape; } try { name = String.valueOf(Character.toChars(keyCode)); } catch (IllegalArgumentException ignored) { } } return name != null ? "<" + prefix + name + ">" : "<<" + key.toString() + ">>"; }
protected ActionCallback _execute(final PlaybackContext context) { final String actionName = getText().substring(PREFIX.length()).trim(); final ActionManager am = ActionManager.getInstance(); final AnAction targetAction = am.getAction(actionName); if (targetAction == null) { dumpError(context, "Unknown action: " + actionName); return new ActionCallback.Rejected(); } if (!context.isUseDirectActionCall()) { final Shortcut[] sc = KeymapManager.getInstance().getActiveKeymap().getShortcuts(actionName); KeyStroke stroke = null; for (Shortcut each : sc) { if (each instanceof KeyboardShortcut) { final KeyboardShortcut ks = (KeyboardShortcut) each; final KeyStroke first = ks.getFirstKeyStroke(); final KeyStroke second = ks.getSecondKeyStroke(); if (first != null && second == null) { stroke = KeyStroke.getKeyStroke(first.getKeyCode(), first.getModifiers(), false); break; } } } if (stroke != null) { final ActionCallback result = new TimedOutCallback( Registry.intValue("actionSystem.commandProcessingTimeout"), "Timed out calling action id=" + actionName, new Throwable(), true) { @Override protected void dumpError() { context.error(getMessage(), getLine()); } }; context.message("Invoking action via shortcut: " + stroke.toString(), getLine()); final KeyStroke finalStroke = stroke; IdeFocusManager.getGlobalInstance() .doWhenFocusSettlesDown( new Runnable() { @Override public void run() { final Ref<AnActionListener> listener = new Ref<AnActionListener>(); listener.set( new AnActionListener.Adapter() { @Override public void beforeActionPerformed( final AnAction action, DataContext dataContext, AnActionEvent event) { SwingUtilities.invokeLater( new Runnable() { @Override public void run() { if (context.isDisposed()) { am.removeAnActionListener(listener.get()); return; } if (targetAction.equals(action)) { context.message( "Performed action: " + actionName, context.getCurrentLine()); am.removeAnActionListener(listener.get()); result.setDone(); } } }); } }); am.addAnActionListener(listener.get()); context.runPooledThread( new Runnable() { @Override public void run() { type(context.getRobot(), finalStroke); } }); } }); return result; } } final InputEvent input = getInputEvent(actionName); final ActionCallback result = new ActionCallback(); context.getRobot().delay(Registry.intValue("actionSystem.playback.delay")); SwingUtilities.invokeLater( new Runnable() { public void run() { am.tryToExecute(targetAction, input, null, null, false) .doWhenProcessed(result.createSetDoneRunnable()); } }); return result; }
/** * Creates a <code>String</code> that keeps the attributes given <code>KeyStroke</code>. This * <code>String</code> can be used to store the key stroke in an XML-file. * * @param keyStroke The <code>KeyStroke</code>. * @return A <code>String</code> that produces a key stroke equal to the given <code>KeyStroke * </code> if passed as a parameter to <code>getAWTKeyStroke(String)</code>. */ public static String getKeyStrokeText(KeyStroke keyStroke) { return (keyStroke == null) ? "" : keyStroke.toString(); }
private static String toString(final KeyStroke newAccelerator) { return newAccelerator.toString().replaceFirst("pressed ", ""); }