Example usage for javax.swing KeyStroke getKeyStroke

List of usage examples for javax.swing KeyStroke getKeyStroke

Introduction

In this page you can find the example usage for javax.swing KeyStroke getKeyStroke.

Prototype

public static KeyStroke getKeyStroke(String s) 

Source Link

Document

Parses a string and returns a KeyStroke.

Usage

From source file:Main.java

public static void main(String[] argv) {
    JButton component = new JButton();
    PrevFocusAction prevFocusAction = new PrevFocusAction();
    component.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke("shift F2"),
            prevFocusAction.getValue(Action.NAME));
    component.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke("shift SPACE"),
            prevFocusAction.getValue(Action.NAME));
    component.getActionMap().put(prevFocusAction.getValue(Action.NAME), prevFocusAction);
}

From source file:Main.java

public static void main(String[] argv) {
    JButton component = new JButton();
    NextFocusAction nextFocusAction = new NextFocusAction();

    component.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke("SPACE"),
            nextFocusAction.getValue(Action.NAME));

    component.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke("F2"),
            nextFocusAction.getValue(Action.NAME));

    component.getActionMap().put(nextFocusAction.getValue(Action.NAME), nextFocusAction);

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JButton component = new JButton("a");

    Set<AWTKeyStroke> set = new HashSet<AWTKeyStroke>(
            component.getFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS));

    set.add(KeyStroke.getKeyStroke("F2"));
    component.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, set);

}

From source file:Main.java

public static void main(String[] argv) {
    InputMap im = new JTextArea().getInputMap(JComponent.WHEN_FOCUSED);
    im.put(KeyStroke.getKeyStroke("F2"), "actionName");

    ActionMap am = new JTextArea().getActionMap();
    am.put("actionName", new AbstractAction("actionName") {
        public void actionPerformed(ActionEvent evt) {
            System.out.println((JTextComponent) evt.getSource());
        }//w w  w . jav a 2  s.c  om
    });
    im.put(KeyStroke.getKeyStroke("F3"), "actionName2");
    am.put("actionName2", new AbstractAction("actionName2") {
        public void actionPerformed(ActionEvent evt) {
            System.out.println((JTextComponent) evt.getSource());
        }
    });
    JButton component1 = new JButton("button 1");
    JButton component2 = new JButton("button 2");

    component1.setInputMap(JComponent.WHEN_FOCUSED, im);
    component2.setInputMap(JComponent.WHEN_FOCUSED, im);

    component1.setActionMap(am);
    component2.setActionMap(am);

}

From source file:Main.java

public static void main(final String args[]) {
    JFrame frame = new JFrame("MenuSample Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JMenuBar menuBar = new JMenuBar();

    // File Menu, F - Mnemonic
    JMenu fileMenu = new JMenu("File");
    fileMenu.setAccelerator(KeyStroke.getKeyStroke("F"));

    // File->New, N - Mnemonic
    JMenuItem newMenuItem = new JMenuItem("New");
    fileMenu.add(newMenuItem);//  w w w .  ja va  2  s.  co m

    frame.setJMenuBar(menuBar);
    frame.setSize(350, 250);
    frame.setVisible(true);

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JTextField component = new JTextField(10);

    // Override letter a
    component.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke("typed a"), "actionName");

    component.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(new Character(' '), 0),
            "actionName");

    component.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke("typed X"), "none");

    component.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke("shift pressed SPACE"),
            "actionName");

    component.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(new Character(' '), 0), "none");

    MyAction action = new MyAction();

    component.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke("pressed SPACE"),
            action.getValue(Action.NAME));

    component.getActionMap().put(action.getValue(Action.NAME), action);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JTextArea comp = new JTextArea();
    String actionName = "Lowercase";

    JFrame f = new JFrame();
    f.add(new JScrollPane(comp));
    f.setSize(300, 300);/*from   ww w .  j a v a 2 s  .c om*/
    f.setVisible(true);
    comp.getInputMap().put(KeyStroke.getKeyStroke("F2"), actionName);

    comp.getActionMap().put(actionName, new TextAction(actionName) {
        public void actionPerformed(ActionEvent evt) {
            JTextComponent comp = getTextComponent(evt);

            if (comp.getSelectionStart() == comp.getSelectionEnd()) {
                if (comp.getCaretPosition() < comp.getDocument().getLength()) {
                    try {
                        int pos = comp.getCaretPosition();
                        Document doc = comp.getDocument();
                        String str = doc.getText(pos, 1).toLowerCase();

                        doc.remove(pos, 1);
                        doc.insertString(pos, str, null);
                        comp.moveCaretPosition(pos + 1);
                    } catch (Exception e) {
                        System.out.println();
                    }
                }
            } else {
                int s = comp.getSelectionStart();
                int e = comp.getSelectionEnd();

                comp.replaceSelection(comp.getSelectedText().toLowerCase());
                comp.select(s, e);
            }
        }
    });
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.setLayout(new BorderLayout());
    frame.add(new JLabel("Hit Escape to exit full screen", JLabel.CENTER), BorderLayout.CENTER);

    frame.setSize(300, 300);//from w  ww.  j a  va  2s .  com

    KeyStroke escapeKeyStroke = KeyStroke.getKeyStroke("ESCAPE");
    Action escapeAction = new AbstractAction() {
        public void actionPerformed(ActionEvent e) {
            GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice()
                    .setFullScreenWindow(null);
        }
    };

    frame.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(escapeKeyStroke, "ESCAPE");
    frame.getRootPane().getActionMap().put("ESCAPE", escapeAction);

    GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(frame);

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    final Action action = new AbstractAction("Action Name") {
        public void actionPerformed(ActionEvent evt) {
            System.out.println("action");
        }//ww w .jav  a2s  .  co  m
    };

    JFrame frame = new JFrame();
    JButton button = new JButton(action);

    JTextField textfield = new JTextField();
    textfield.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke("F2"),
            action.getValue(Action.NAME));
    textfield.getActionMap().put(action.getValue(Action.NAME), action);
}

From source file:MainClass.java

public static void main(final String args[]) {
    final JFrame frame = new JFrame("Frame Key");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Action actionListener = new AbstractAction() {
        public void actionPerformed(ActionEvent actionEvent) {
            System.out.println("Got an M");
        }//from  w w w  .j a  v  a 2s.c  o m
    };

    JPanel content = (JPanel) frame.getContentPane();
    KeyStroke stroke = KeyStroke.getKeyStroke("M");

    InputMap inputMap = content.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    inputMap.put(stroke, "OPEN");
    content.getActionMap().put("OPEN", actionListener);

    frame.setSize(300, 300);
    frame.setVisible(true);
}