Example usage for java.awt.event InputEvent CTRL_MASK

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

Introduction

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

Prototype

int CTRL_MASK

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

Click Source Link

Document

The Control key modifier constant.

Usage

From source file:KeymapExample.java

public static void main(String[] args) {
    JTextArea area = new JTextArea(6, 32);
    Keymap parent = area.getKeymap();
    Keymap newmap = JTextComponent.addKeymap("KeymapExampleMap", parent);

    KeyStroke u = KeyStroke.getKeyStroke(KeyEvent.VK_U, InputEvent.CTRL_MASK);
    Action actionU = new UpWord();
    newmap.addActionForKeyStroke(u, actionU);

    Action actionList[] = area.getActions();
    Hashtable lookup = new Hashtable();
    for (int j = 0; j < actionList.length; j += 1)
        lookup.put(actionList[j].getValue(Action.NAME), actionList[j]);

    KeyStroke L = KeyStroke.getKeyStroke(KeyEvent.VK_L, InputEvent.CTRL_MASK);
    Action actionL = (Action) lookup.get(DefaultEditorKit.selectLineAction);
    newmap.addActionForKeyStroke(L, actionL);

    KeyStroke W = KeyStroke.getKeyStroke(KeyEvent.VK_W, InputEvent.CTRL_MASK);
    Action actionW = (Action) lookup.get(DefaultEditorKit.selectWordAction);
    newmap.addActionForKeyStroke(W, actionW);

    area.setKeymap(newmap);/*from  www.  j a  v a 2s  .com*/

    JFrame f = new JFrame("KeymapExample");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(new JScrollPane(area), BorderLayout.CENTER);
    area.setText("www.\n java2s \n .com.");
    f.pack();
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    String[] items = { "Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat" };
    JList<String> myJList = new JList(items) {
        @Override/* w  w  w .java2s. c o  m*/
        protected void processMouseEvent(MouseEvent e) {
            int modifiers = e.getModifiers() | InputEvent.CTRL_MASK;
            int modifiersEx = e.getModifiersEx() | InputEvent.CTRL_MASK;
            MouseEvent myME = new MouseEvent((Component) e.getSource(), e.getID(), e.getWhen(), modifiers,
                    e.getX(), e.getY(), e.getXOnScreen(), e.getYOnScreen(), e.getClickCount(),
                    e.isPopupTrigger(), e.getButton());
            super.processMouseEvent(myME);
        }
    };
    JFrame f = new JFrame();
    f.add(new JScrollPane(myJList));
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.pack();
    f.setLocationRelativeTo(null);
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JTextArea area = new JTextArea(6, 32);
    Keymap parent = area.getKeymap();
    Keymap newmap = JTextComponent.addKeymap("KeymapExampleMap", parent);

    KeyStroke u = KeyStroke.getKeyStroke(KeyEvent.VK_U, InputEvent.CTRL_MASK);
    Action actionU = new UppercaseAction();
    newmap.addActionForKeyStroke(u, actionU);

    Action actionList[] = area.getActions();
    Hashtable lookup = new Hashtable();
    for (int j = 0; j < actionList.length; j += 1)
        lookup.put(actionList[j].getValue(Action.NAME), actionList[j]);

    KeyStroke L = KeyStroke.getKeyStroke(KeyEvent.VK_L, InputEvent.CTRL_MASK);
    Action actionL = (Action) lookup.get(DefaultEditorKit.selectLineAction);
    newmap.addActionForKeyStroke(L, actionL);

    area.setKeymap(newmap);//from   w  w  w.  j  av  a2s  .  c  om

    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.add(new JScrollPane(area), BorderLayout.CENTER);
    area.setText("this is a test");
    f.setSize(300, 300);
    f.setVisible(true);
}

From source file:Main.java

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

    JMenuBar menuBar = new JMenuBar();
    JMenu menu = new JMenu("File");
    menuBar.add(menu);//from ww  w. j  a  v a2  s.  co  m
    JMenuItem menuItem = new JMenuItem("Print");
    KeyStroke ctrlP = KeyStroke.getKeyStroke(KeyEvent.VK_P, InputEvent.CTRL_MASK);
    menuItem.setAccelerator(ctrlP);
    menuItem.addActionListener(printAction);
    menu.add(menuItem);

    JButton fileButton = new JButton("About");
    fileButton.setMnemonic(KeyEvent.VK_A);
    fileButton.addActionListener(printAction);

    frame.setJMenuBar(menuBar);

    frame.add(fileButton, BorderLayout.SOUTH);
    frame.setSize(300, 100);
    frame.setVisible(true);
}

From source file:KeymapExample.java

public static void main(String[] args) {
    JTextArea area = new JTextArea(6, 32);
    Keymap parent = area.getKeymap();
    Keymap newmap = JTextComponent.addKeymap("KeymapExampleMap", parent);

    KeyStroke u = KeyStroke.getKeyStroke(KeyEvent.VK_U, InputEvent.CTRL_MASK);
    Action actionU = new UpWord();
    newmap.addActionForKeyStroke(u, actionU);

    Action actionList[] = area.getActions();
    Hashtable lookup = new Hashtable();
    for (int j = 0; j < actionList.length; j += 1)
        lookup.put(actionList[j].getValue(Action.NAME), actionList[j]);

    KeyStroke L = KeyStroke.getKeyStroke(KeyEvent.VK_L, InputEvent.CTRL_MASK);
    Action actionL = (Action) lookup.get(DefaultEditorKit.selectLineAction);
    newmap.addActionForKeyStroke(L, actionL);

    KeyStroke W = KeyStroke.getKeyStroke(KeyEvent.VK_W, InputEvent.CTRL_MASK);
    Action actionW = (Action) lookup.get(DefaultEditorKit.selectWordAction);
    newmap.addActionForKeyStroke(W, actionW);

    area.setKeymap(newmap);/*w w  w  . ja  v a 2  s. c  o  m*/

    JFrame f = new JFrame("KeymapExample");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(new JScrollPane(area), BorderLayout.CENTER);
    area.setText("This is a test.");
    f.pack();
    f.setVisible(true);
}

From source file:ScreenDump.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Action Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final Action printAction = new PrintHelloAction();

    JMenuBar menuBar = new JMenuBar();
    JMenu menu = new JMenu("File");
    menuBar.add(menu);//from w  ww  .  j  a v  a 2s  .  co m
    JMenuItem menuItem = new JMenuItem("Print");
    KeyStroke ctrlP = KeyStroke.getKeyStroke(KeyEvent.VK_P, InputEvent.CTRL_MASK);
    menuItem.setAccelerator(ctrlP);
    menuItem.addActionListener(printAction);
    menu.add(menuItem);

    JButton fileButton = new JButton("About");
    fileButton.setMnemonic(KeyEvent.VK_A);
    fileButton.addActionListener(printAction);

    frame.setJMenuBar(menuBar);

    Container contentPane = frame.getContentPane();
    contentPane.add(fileButton, BorderLayout.SOUTH);
    frame.setSize(300, 100);
    frame.setVisible(true);
}

From source file:MainClass.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Mnemonic/Accelerator Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final Action printAction = new PrintHelloAction();

    JMenuBar menuBar = new JMenuBar();
    JMenu menu = new JMenu("File");
    menuBar.add(menu);/*w  w  w. jav a2s  . co m*/
    JMenuItem menuItem = new JMenuItem("Print");
    KeyStroke ctrlP = KeyStroke.getKeyStroke(KeyEvent.VK_P, InputEvent.CTRL_MASK);
    menuItem.setAccelerator(ctrlP);
    menuItem.addActionListener(printAction);
    menu.add(menuItem);

    JButton fileButton = new JButton("About");
    fileButton.setMnemonic(KeyEvent.VK_A);
    fileButton.addActionListener(printAction);

    frame.setJMenuBar(menuBar);

    frame.add(fileButton, BorderLayout.SOUTH);
    frame.setSize(300, 100);
    frame.setVisible(true);
}

From source file:Main.java

private static int keyStrokeModMac(String keyStrokeStr) {
    keyStrokeStr = keyStrokeStr.toLowerCase();
    int mod = 0;/*from   w w w  .j a v  a2 s . 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 ww  w  .  j a v  a  2  s.  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 ww  w.j a  v  a 2 s .co 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();
}