Comment puis-je mettre en œuvre Ctrl+Z / Commande+Z en Java/Swing?

Je travaille sur un petit applet Java qui doit annuler/rétablir la fonctionnalité. Voici le code pour configurer les touches de raccourci (fonctionne très bien sur Windows).

Ma question est: comment puis-je faire utiliser la commande+Z sur mac? Dois-je viens de vérifier le Système.getProperty("os.name") ou est-il plus élégante 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);
    }
}

Merci,

Neal

OriginalL'auteur Neal Ehardt | 2010-12-16