I'm working on a little Java applet that needs undo/redo functionality. Here's code to set up the hotkeys (works great on Windows).
My question is: how do I make it use command+Z on mac? Should I just check System.getProperty("os.name") or is there a more elegant alternative??
private void setupUndoHotkeys() {
String UNDO = "Undo action key";
String REDO = "Redo action key";
Action undoAction = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
undo();
}
};
Action redoAction = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
redo();
}
};
getActionMap().put(UNDO, undoAction);
getActionMap().put(REDO, redoAction);
InputMap[] inputMaps = new InputMap[] {
getInputMap(JComponent.WHEN_FOCUSED),
getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT),
getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW),
};
for(InputMap i : inputMaps) {
i.put(KeyStroke.getKeyStroke("control Z"), UNDO);
i.put(KeyStroke.getKeyStroke("control Y"), REDO);
}
}
Thanks,
Neal
It terminates your program. ctrl z is used to pause the process.
To undo an action, press Ctrl + Z. To redo an undone action, press Ctrl + Y. The Undo and Redo features let you remove or repeat single or multiple typing actions, but all actions must be undone or redone in the order you did or undid them – you can't skip actions.
When the user selects undo, we perform the undo operation of the current item in the list and then step backwards to the previous item. To redo an operation, we execute the redo operation of the next item in the list and step forward.
Ah nevermind, I found it here http://www.devdaily.com/blog/post/jfc-swing/how-program-apple-command-key-keystroke-java-swing-mac-osx
This should be undo on any platform.
KeyStroke.getKeyStroke(KeyEvent.VK_Z, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With