Example usage for java.awt.event InputEvent ALT_MASK

List of usage examples for java.awt.event InputEvent ALT_MASK

Introduction

In this page you can find the example usage for java.awt.event InputEvent ALT_MASK.

Prototype

int ALT_MASK

To view the source code for java.awt.event InputEvent ALT_MASK.

Click Source Link

Document

The Alt key modifier constant.

Usage

From source file:Main.java

private static int keyStrokeModMac(String keyStrokeStr) {
    keyStrokeStr = keyStrokeStr.toLowerCase();
    int mod = 0;//  w ww.  j a  v  a2s  .c  o m
    if (keyStrokeStr.contains("ctrl") || keyStrokeStr.contains("control")) {
        mod = mod | InputEvent.CTRL_MASK;
    }
    if (keyStrokeStr.contains("alt")) {
        mod = mod | Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
    }
    if (keyStrokeStr.contains("meta")) {
        mod = mod | InputEvent.ALT_MASK;
    }
    return mod;
}

From source file:Main.java

/**
 * Serializes a Keystrike to a String./*from   www  . ja v  a 2s . c  o m*/
 * It can then be fed into KeyStroke.getKeyStroke(String).
 *
 * See: http://www.exampledepot.com/egs/javax.swing/Key2Str.html
 */
public static String keyStroke2String(KeyStroke key) {
    StringBuffer s = new StringBuffer(50);
    int m = key.getModifiers();

    if ((m & (InputEvent.SHIFT_DOWN_MASK | InputEvent.SHIFT_MASK)) != 0) {
        s.append("shift ");
    }
    if ((m & (InputEvent.CTRL_DOWN_MASK | InputEvent.CTRL_MASK)) != 0) {
        s.append("ctrl ");
    }
    if ((m & (InputEvent.META_DOWN_MASK | InputEvent.META_MASK)) != 0) {
        s.append("meta ");
    }
    if ((m & (InputEvent.ALT_DOWN_MASK | InputEvent.ALT_MASK)) != 0) {
        s.append("alt ");
    }
    if ((m & (InputEvent.BUTTON1_DOWN_MASK | InputEvent.BUTTON1_MASK)) != 0) {
        s.append("button1 ");
    }
    if ((m & (InputEvent.BUTTON2_DOWN_MASK | InputEvent.BUTTON2_MASK)) != 0) {
        s.append("button2 ");
    }
    if ((m & (InputEvent.BUTTON3_DOWN_MASK | InputEvent.BUTTON3_MASK)) != 0) {
        s.append("button3 ");
    }

    switch (key.getKeyEventType()) {
    case KeyEvent.KEY_TYPED:
        if (key.getKeyChar() == Character.MAX_VALUE) {
            s.append("unknown-typed-key ");
            break;
        }
        s.append("typed ").append(key.getKeyChar()).append(" ");
        break;
    case KeyEvent.KEY_PRESSED:
        s.append("pressed ").append(getKeyText(key.getKeyCode())).append(" ");
        break;
    case KeyEvent.KEY_RELEASED:
        s.append("released ").append(getKeyText(key.getKeyCode())).append(" ");
        break;
    default:
        s.append("unknown-event-type ");
        break;
    }

    return s.toString();
}

From source file:KeystrokeUtil.java

public static String keyStroke2String(KeyStroke key) {
    if (key == null)
        return "";
    StringBuilder s = new StringBuilder(50);
    int m = key.getModifiers();

    if ((m & (InputEvent.CTRL_DOWN_MASK | InputEvent.CTRL_MASK)) != 0) {
        s.append("Ctrl+");
    }//from  www  .j a  v  a 2s .  c o  m
    if ((m & (InputEvent.META_DOWN_MASK | InputEvent.META_MASK)) != 0) {
        s.append("Meta+");
    }
    if ((m & (InputEvent.ALT_DOWN_MASK | InputEvent.ALT_MASK)) != 0) {
        s.append("Alt+");
    }
    if ((m & (InputEvent.SHIFT_DOWN_MASK | InputEvent.SHIFT_MASK)) != 0) {
        s.append("Shift+");
    }
    if ((m & (InputEvent.BUTTON1_DOWN_MASK | InputEvent.BUTTON1_MASK)) != 0) {
        s.append("Button1+");
    }
    if ((m & (InputEvent.BUTTON2_DOWN_MASK | InputEvent.BUTTON2_MASK)) != 0) {
        s.append("Button2+");
    }
    if ((m & (InputEvent.BUTTON3_DOWN_MASK | InputEvent.BUTTON3_MASK)) != 0) {
        s.append("Button3+");
    }

    switch (key.getKeyEventType()) {
    case KeyEvent.KEY_TYPED:
        s.append(key.getKeyChar() + " ");
        break;
    case KeyEvent.KEY_PRESSED:
    case KeyEvent.KEY_RELEASED:
        s.append(getKeyText(key.getKeyCode()) + " ");
        break;
    default:
        s.append("unknown-event-type ");
        break;
    }

    return s.toString();
}

From source file:Main.java

/**
 * Returns whether ALT modifier is triggered by the specified modifiers or
 * not./* w w  w . j a v  a 2  s . co m*/
 *
 * @param modifiers
 *            modifiers to process
 * @return true if ALT modifier is triggered by the specified modifiers,
 *         false otherwise
 */
public static boolean isAlt(final int modifiers) {
    return (modifiers & InputEvent.ALT_MASK) != 0;
}

From source file:Main.java

public Main(String title) {
    super(title);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JMenuBar menuBar = new JMenuBar();
    tabComponentsItem = new JCheckBoxMenuItem("Use TabComponents", true);
    tabComponentsItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_T, InputEvent.ALT_MASK));
    tabComponentsItem.addActionListener(e -> {
        for (int i = 0; i < pane.getTabCount(); i++) {
            if (tabComponentsItem.isSelected()) {
            } else {
                pane.setTabComponentAt(i, null);
            }//w w w  .j av  a2 s .c o m
        }
    });
    scrollLayoutItem = new JCheckBoxMenuItem("Set ScrollLayout");
    scrollLayoutItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.ALT_MASK));
    scrollLayoutItem.addActionListener(e -> {
        if (pane.getTabLayoutPolicy() == JTabbedPane.WRAP_TAB_LAYOUT) {
            pane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
        } else {
            pane.setTabLayoutPolicy(JTabbedPane.WRAP_TAB_LAYOUT);
        }
    });
    JMenuItem resetItem = new JMenuItem("Reset JTabbedPane");
    resetItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R, InputEvent.ALT_MASK));
    resetItem.addActionListener(e -> runTest());
    JMenu optionsMenu = new JMenu("Options");
    optionsMenu.add(tabComponentsItem);
    optionsMenu.add(scrollLayoutItem);
    optionsMenu.add(resetItem);
    menuBar.add(optionsMenu);
    setJMenuBar(menuBar);
    add(pane);
}

From source file:Main.java

static void keyStroke2String(KeyStroke key) {
    int m = key.getModifiers();

    if ((m & (InputEvent.SHIFT_DOWN_MASK | InputEvent.SHIFT_MASK)) != 0) {
        System.out.println("shift ");
    }//  www .  j a  va2  s .c  om
    if ((m & (InputEvent.CTRL_DOWN_MASK | InputEvent.CTRL_MASK)) != 0) {
        System.out.println("ctrl ");
    }
    if ((m & (InputEvent.META_DOWN_MASK | InputEvent.META_MASK)) != 0) {
        System.out.println("meta ");
    }
    if ((m & (InputEvent.ALT_DOWN_MASK | InputEvent.ALT_MASK)) != 0) {
        System.out.println("alt ");
    }
    if ((m & (InputEvent.BUTTON1_DOWN_MASK | InputEvent.BUTTON1_MASK)) != 0) {
        System.out.println("button1 ");
    }
    if ((m & (InputEvent.BUTTON2_DOWN_MASK | InputEvent.BUTTON2_MASK)) != 0) {
        System.out.println("button2 ");
    }
    if ((m & (InputEvent.BUTTON3_DOWN_MASK | InputEvent.BUTTON3_MASK)) != 0) {
        System.out.println("button3 ");
    }
    switch (key.getKeyEventType()) {
    case KeyEvent.KEY_TYPED:
        System.out.println("typed ");
        System.out.println(key.getKeyChar() + " ");
        break;
    case KeyEvent.KEY_PRESSED:
        System.out.println("pressed ");
        System.out.println(getKeyText(key.getKeyCode()) + " ");
        break;
    case KeyEvent.KEY_RELEASED:
        System.out.println("released ");
        System.out.println(getKeyText(key.getKeyCode()) + " ");
        break;
    default:
        System.out.println("unknown-event-type ");
        break;
    }
}

From source file:Main.java

static void keyStroke2String(KeyStroke key) {
    int m = key.getModifiers();

    if ((m & (InputEvent.SHIFT_DOWN_MASK | InputEvent.SHIFT_MASK)) != 0) {
        System.out.println("shift ");
    }/*from w w  w . j  a  va  2s  .c  o  m*/
    if ((m & (InputEvent.CTRL_DOWN_MASK | InputEvent.CTRL_MASK)) != 0) {
        System.out.println("ctrl ");
    }
    if ((m & (InputEvent.META_DOWN_MASK | InputEvent.META_MASK)) != 0) {
        System.out.println("meta ");
    }
    if ((m & (InputEvent.ALT_DOWN_MASK | InputEvent.ALT_MASK)) != 0) {
        System.out.println("alt ");
    }
    if ((m & (InputEvent.BUTTON1_DOWN_MASK | InputEvent.BUTTON1_MASK)) != 0) {
        System.out.println("button1 ");
    }
    if ((m & (InputEvent.BUTTON2_DOWN_MASK | InputEvent.BUTTON2_MASK)) != 0) {
        System.out.println("button2 ");
    }
    if ((m & (InputEvent.BUTTON3_DOWN_MASK | InputEvent.BUTTON3_MASK)) != 0) {
        System.out.println("button3 ");
    }

    switch (key.getKeyEventType()) {
    case KeyEvent.KEY_TYPED:
        System.out.println("typed ");
        System.out.println(key.getKeyChar() + " ");
        break;
    case KeyEvent.KEY_PRESSED:
        System.out.println("pressed ");
        System.out.println(getKeyText(key.getKeyCode()) + " ");
        break;
    case KeyEvent.KEY_RELEASED:
        System.out.println("released ");
        System.out.println(getKeyText(key.getKeyCode()) + " ");
        break;
    default:
        System.out.println("unknown-event-type ");
        break;
    }
}

From source file:Main.java

/**
 * A helper for creating and updating key bindings for components with
 * mnemonics. The {@code pressed} action will be invoked when the mnemonic
 * is activated and the {@code released} action will be invoked when the
 * mnemonic is deactivated.//from   ww  w.j  a v  a  2  s . c  om
 * <p>
 * TODO establish an interface for the mnemonic properties, such as {@code
 * MnemonicEnabled} and change signature to {@code public static <T extends
 * JComponent & MnemonicEnabled> void updateMnemonicBinding(T c, String
 * pressed, String released)}
 * 
 * @param c
 *            the component bindings to update
 * @param pressed
 *            the name of the action in the action map to invoke when the
 *            mnemonic is pressed
 * @param released
 *            the name of the action in the action map to invoke when the
 *            mnemonic is released (if the action is a toggle style, then
 *            this parameter should be {@code null})
 * @throws NullPointerException
 *             if the component is {@code null}
 */
public static void updateMnemonicBinding(JComponent c, String pressed, String released) {
    Class<?> clazz = c.getClass();
    int m = -1;

    try {
        Method mtd = clazz.getMethod("getMnemonic");
        m = (Integer) mtd.invoke(c);
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new IllegalArgumentException("unable to access mnemonic", e);
    }

    InputMap map = SwingUtilities.getUIInputMap(c, JComponent.WHEN_IN_FOCUSED_WINDOW);

    if (m != 0) {
        if (map == null) {
            map = new ComponentInputMapUIResource(c);
            SwingUtilities.replaceUIInputMap(c, JComponent.WHEN_IN_FOCUSED_WINDOW, map);
        }

        map.clear();

        //TODO is ALT_MASK right for all platforms?
        map.put(KeyStroke.getKeyStroke(m, InputEvent.ALT_MASK, false), pressed);
        map.put(KeyStroke.getKeyStroke(m, InputEvent.ALT_MASK, true), released);
        map.put(KeyStroke.getKeyStroke(m, 0, true), released);
    } else {
        if (map != null) {
            map.clear();
        }
    }
}

From source file:components.TabComponentsDemo.java

private void initMenu() {
    JMenuBar menuBar = new JMenuBar();
    //create Options menu
    tabComponentsItem = new JCheckBoxMenuItem("Use TabComponents", true);
    tabComponentsItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_T, InputEvent.ALT_MASK));
    tabComponentsItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            for (int i = 0; i < pane.getTabCount(); i++) {
                if (tabComponentsItem.isSelected()) {
                    initTabComponent(i);
                } else {
                    pane.setTabComponentAt(i, null);
                }//from  w ww .j  a v  a 2  s . com
            }
        }
    });
    scrollLayoutItem = new JCheckBoxMenuItem("Set ScrollLayout");
    scrollLayoutItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.ALT_MASK));
    scrollLayoutItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (pane.getTabLayoutPolicy() == JTabbedPane.WRAP_TAB_LAYOUT) {
                pane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
            } else {
                pane.setTabLayoutPolicy(JTabbedPane.WRAP_TAB_LAYOUT);
            }
        }
    });
    JMenuItem resetItem = new JMenuItem("Reset JTabbedPane");
    resetItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R, InputEvent.ALT_MASK));
    resetItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            runTest();
        }
    });

    JMenu optionsMenu = new JMenu("Options");
    optionsMenu.add(tabComponentsItem);
    optionsMenu.add(scrollLayoutItem);
    optionsMenu.add(resetItem);
    menuBar.add(optionsMenu);
    setJMenuBar(menuBar);
}

From source file:com.mirth.connect.client.ui.components.rsta.RSTAPreferences.java

private void setDefaultKeyStrokeMap() {
    keyStrokeMap = new HashMap<String, KeyStroke>();

    boolean isOSX = RTextArea.isOSX();
    int defaultModifier = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
    int ctrl = InputEvent.CTRL_MASK;
    int alt = InputEvent.ALT_MASK;
    int shift = InputEvent.SHIFT_MASK;
    int defaultShift = defaultModifier | shift;
    int moveByWordMod = isOSX ? alt : defaultModifier;
    int moveByWordModShift = moveByWordMod | shift;

    putKeyStroke(ActionInfo.UNDO, KeyEvent.VK_Z, defaultModifier);

    if (isOSX) {/*from   www.j  av a  2 s  . c  o m*/
        putKeyStroke(ActionInfo.REDO, KeyEvent.VK_Z, defaultShift);
    } else {
        putKeyStroke(ActionInfo.REDO, KeyEvent.VK_Y, defaultModifier);
    }

    putKeyStroke(ActionInfo.CUT, KeyEvent.VK_X, defaultModifier);
    putKeyStroke(ActionInfo.COPY, KeyEvent.VK_C, defaultModifier);
    putKeyStroke(ActionInfo.PASTE, KeyEvent.VK_V, defaultModifier);
    putKeyStroke(ActionInfo.DELETE, KeyEvent.VK_DELETE, 0);
    putKeyStroke(ActionInfo.DELETE_REST_OF_LINE, KeyEvent.VK_DELETE, defaultModifier);
    putKeyStroke(ActionInfo.DELETE_LINE, KeyEvent.VK_D, defaultModifier);
    putKeyStroke(ActionInfo.JOIN_LINE, KeyEvent.VK_J, defaultModifier);
    putKeyStroke(ActionInfo.SELECT_ALL, KeyEvent.VK_A, defaultModifier);
    putKeyStroke(ActionInfo.FIND_REPLACE, KeyEvent.VK_F, defaultModifier);
    putKeyStroke(ActionInfo.FIND_NEXT, KeyEvent.VK_G, defaultModifier);
    putKeyStroke(ActionInfo.CLEAR_MARKED_OCCURRENCES, KeyEvent.VK_ESCAPE, 0);
    putKeyStroke(ActionInfo.FOLD_COLLAPSE, KeyEvent.VK_SUBTRACT, defaultModifier);
    putKeyStroke(ActionInfo.FOLD_EXPAND, KeyEvent.VK_ADD, defaultModifier);
    putKeyStroke(ActionInfo.FOLD_COLLAPSE_ALL, KeyEvent.VK_DIVIDE, defaultModifier);
    putKeyStroke(ActionInfo.FOLD_COLLAPSE_ALL_COMMENTS, KeyEvent.VK_DIVIDE, defaultShift);
    putKeyStroke(ActionInfo.FOLD_EXPAND_ALL, KeyEvent.VK_MULTIPLY, defaultModifier);
    putKeyStroke(ActionInfo.GO_TO_MATCHING_BRACKET, KeyEvent.VK_OPEN_BRACKET, defaultModifier);
    putKeyStroke(ActionInfo.TOGGLE_COMMENT, KeyEvent.VK_SLASH, defaultModifier);
    putKeyStroke(ActionInfo.AUTO_COMPLETE, KeyEvent.VK_SPACE, ctrl);

    if (isOSX) {
        putKeyStroke(ActionInfo.DOCUMENT_START, KeyEvent.VK_HOME, 0);
        putKeyStroke(ActionInfo.DOCUMENT_END, KeyEvent.VK_END, 0);
        putKeyStroke(ActionInfo.DOCUMENT_SELECT_START, KeyEvent.VK_HOME, shift);
        putKeyStroke(ActionInfo.DOCUMENT_SELECT_END, KeyEvent.VK_END, shift);
        putKeyStroke(ActionInfo.LINE_START, KeyEvent.VK_LEFT, defaultModifier);
        putKeyStroke(ActionInfo.LINE_END, KeyEvent.VK_RIGHT, defaultModifier);
        putKeyStroke(ActionInfo.LINE_SELECT_START, KeyEvent.VK_LEFT, defaultShift);
        putKeyStroke(ActionInfo.LINE_SELECT_END, KeyEvent.VK_RIGHT, defaultShift);
    } else {
        putKeyStroke(ActionInfo.DOCUMENT_START, KeyEvent.VK_HOME, defaultModifier);
        putKeyStroke(ActionInfo.DOCUMENT_END, KeyEvent.VK_END, defaultModifier);
        putKeyStroke(ActionInfo.DOCUMENT_SELECT_START, KeyEvent.VK_HOME, defaultShift);
        putKeyStroke(ActionInfo.DOCUMENT_SELECT_END, KeyEvent.VK_END, defaultShift);
        putKeyStroke(ActionInfo.LINE_START, KeyEvent.VK_HOME, 0);
        putKeyStroke(ActionInfo.LINE_END, KeyEvent.VK_END, 0);
        putKeyStroke(ActionInfo.LINE_SELECT_START, KeyEvent.VK_HOME, shift);
        putKeyStroke(ActionInfo.LINE_SELECT_END, KeyEvent.VK_END, shift);
    }

    putKeyStroke(ActionInfo.MOVE_LEFT, KeyEvent.VK_LEFT, 0);
    putKeyStroke(ActionInfo.MOVE_LEFT_SELECT, KeyEvent.VK_LEFT, shift);
    putKeyStroke(ActionInfo.MOVE_LEFT_WORD, KeyEvent.VK_LEFT, moveByWordMod);
    putKeyStroke(ActionInfo.MOVE_LEFT_WORD_SELECT, KeyEvent.VK_LEFT, moveByWordModShift);
    putKeyStroke(ActionInfo.MOVE_RIGHT, KeyEvent.VK_RIGHT, 0);
    putKeyStroke(ActionInfo.MOVE_RIGHT_SELECT, KeyEvent.VK_RIGHT, shift);
    putKeyStroke(ActionInfo.MOVE_RIGHT_WORD, KeyEvent.VK_RIGHT, moveByWordMod);
    putKeyStroke(ActionInfo.MOVE_RIGHT_WORD_SELECT, KeyEvent.VK_RIGHT, moveByWordModShift);
    putKeyStroke(ActionInfo.MOVE_UP, KeyEvent.VK_UP, 0);
    putKeyStroke(ActionInfo.MOVE_UP_SELECT, KeyEvent.VK_UP, shift);
    putKeyStroke(ActionInfo.MOVE_UP_SCROLL, KeyEvent.VK_UP, defaultModifier);
    putKeyStroke(ActionInfo.MOVE_UP_LINE, KeyEvent.VK_UP, alt);
    putKeyStroke(ActionInfo.MOVE_DOWN, KeyEvent.VK_DOWN, 0);
    putKeyStroke(ActionInfo.MOVE_DOWN_SELECT, KeyEvent.VK_DOWN, shift);
    putKeyStroke(ActionInfo.MOVE_DOWN_SCROLL, KeyEvent.VK_DOWN, defaultModifier);
    putKeyStroke(ActionInfo.MOVE_DOWN_LINE, KeyEvent.VK_DOWN, alt);
    putKeyStroke(ActionInfo.PAGE_UP, KeyEvent.VK_PAGE_UP, 0);
    putKeyStroke(ActionInfo.PAGE_UP_SELECT, KeyEvent.VK_PAGE_UP, shift);
    putKeyStroke(ActionInfo.PAGE_LEFT_SELECT, KeyEvent.VK_PAGE_UP, defaultShift);
    putKeyStroke(ActionInfo.PAGE_DOWN, KeyEvent.VK_PAGE_DOWN, 0);
    putKeyStroke(ActionInfo.PAGE_DOWN_SELECT, KeyEvent.VK_PAGE_DOWN, shift);
    putKeyStroke(ActionInfo.PAGE_RIGHT_SELECT, KeyEvent.VK_PAGE_DOWN, defaultShift);
    putKeyStroke(ActionInfo.INSERT_LF_BREAK, KeyEvent.VK_ENTER, 0);
    putKeyStroke(ActionInfo.INSERT_CR_BREAK, KeyEvent.VK_ENTER, shift);
    putKeyStroke(ActionInfo.MACRO_BEGIN, KeyEvent.VK_B, defaultShift);
    putKeyStroke(ActionInfo.MACRO_END, KeyEvent.VK_N, defaultShift);
    putKeyStroke(ActionInfo.MACRO_PLAYBACK, KeyEvent.VK_M, defaultShift);
}