Example usage for java.awt.event KeyEvent getModifiers

List of usage examples for java.awt.event KeyEvent getModifiers

Introduction

In this page you can find the example usage for java.awt.event KeyEvent getModifiers.

Prototype

@Deprecated(since = "9")
public int getModifiers() 

Source Link

Document

Returns the modifier mask for this event.

Usage

From source file:Main.java

public static boolean isPasteAction() {
    AWTEvent e = EventQueue.getCurrentEvent();
    if (e != null && e.getID() == KeyEvent.KEY_PRESSED) {
        KeyEvent k = (KeyEvent) e;
        if (k.getKeyCode() == KeyEvent.VK_V && k.getModifiers() == KeyEvent.CTRL_MASK) {
            return true;
        }/*w w w  .j ava2s .c  om*/
    }
    return false;
}

From source file:Main.java

/**
 * Returns true if the given event is corrent gesture for
 * accessing clipboard//from w w  w  . j a  v a 2 s  .c o  m
 *
 * @param ie InputEvent to check
 */

private static boolean isAccessClipboardGesture(InputEvent ie) {
    boolean allowedGesture = false;
    if (ie instanceof KeyEvent) { //we can validate only keyboard gestures
        KeyEvent ke = (KeyEvent) ie;
        int keyCode = ke.getKeyCode();
        int keyModifiers = ke.getModifiers();
        switch (keyCode) {
        case KeyEvent.VK_C:
        case KeyEvent.VK_V:
        case KeyEvent.VK_X:
            allowedGesture = (keyModifiers == InputEvent.CTRL_MASK);
            break;
        case KeyEvent.VK_INSERT:
            allowedGesture = (keyModifiers == InputEvent.CTRL_MASK || keyModifiers == InputEvent.SHIFT_MASK);
            break;
        case KeyEvent.VK_COPY:
        case KeyEvent.VK_PASTE:
        case KeyEvent.VK_CUT:
            allowedGesture = true;
            break;
        case KeyEvent.VK_DELETE:
            allowedGesture = (keyModifiers == InputEvent.SHIFT_MASK);
            break;
        }
    }
    return allowedGesture;
}

From source file:com.haulmont.cuba.desktop.gui.components.DesktopFilterHelper.java

@Override
public void addShortcutListener(TextField textField, final ShortcutListener listener) {
    final JTextField dTextField = (JTextField) DesktopComponentsHelper.unwrap(textField);
    final KeyStroke keyStroke = DesktopComponentsHelper.convertKeyCombination(listener.getKeyCombination());
    dTextField.addKeyListener(new KeyAdapter() {
        @Override//from   w  w  w.j av a 2  s .co  m
        public void keyReleased(KeyEvent e) {
            if (ObjectUtils.equals(e.getKeyCode(), keyStroke.getKeyCode())
                    && ObjectUtils.equals(e.getModifiers(), keyStroke.getModifiers())) {
                listener.handleShortcutPressed();
            }
        }
    });
}

From source file:com.github.alexfalappa.nbspringboot.cfgprops.completion.CfgPropCompletionItem.java

@Override
public void processKeyEvent(KeyEvent evt) {
    // detect if Ctrl + Enter is pressed
    overwrite = evt.getKeyCode() == KeyEvent.VK_ENTER && (evt.getModifiers() & KeyEvent.CTRL_MASK) != 0;
}

From source file:com.github.fritaly.dualcommander.TabbedPane.java

@Override
public void keyReleased(KeyEvent e) {
    if (e.getSource() == getSelectedComponent()) {
        // Propagate the event to our listeners
        processKeyEvent(new KeyEvent(this, e.getID(), e.getWhen(), e.getModifiers(), e.getKeyCode(),
                e.getKeyChar(), e.getKeyLocation()));
    }//from w w  w.j  a v  a 2 s  .  c o  m
}

From source file:com.github.fritaly.dualcommander.TabbedPane.java

@Override
public void keyTyped(KeyEvent e) {
    if (e.getSource() == getSelectedComponent()) {
        // Propagate the event to our listeners
        processKeyEvent(new KeyEvent(this, e.getID(), e.getWhen(), e.getModifiers(), e.getKeyCode(),
                e.getKeyChar(), e.getKeyLocation()));
    }/*from  w  w w.j a v  a  2s . c o m*/
}

From source file:com.haulmont.cuba.desktop.gui.components.DesktopTextArea.java

@Override
protected JTextArea createTextComponentImpl() {
    final JTextArea impl = new TextAreaFlushableField();

    if (isTabTraversal()) {
        Set<KeyStroke> forwardFocusKey = Collections.singleton(getKeyStroke(KeyEvent.VK_TAB, 0));
        impl.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, forwardFocusKey);

        Set<KeyStroke> backwardFocusKey = Collections
                .singleton(getKeyStroke(KeyEvent.VK_TAB, KeyEvent.SHIFT_MASK));
        impl.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, backwardFocusKey);

        impl.addKeyListener(new KeyAdapter() {
            @Override// w w  w  .  j  a va 2s  . co  m
            public void keyPressed(KeyEvent e) {
                if (isEnabled() && isEditable() && e.getKeyCode() == KeyEvent.VK_TAB
                        && e.getModifiers() == KeyEvent.CTRL_MASK) {

                    if (StringUtils.isEmpty(impl.getText())) {
                        impl.setText("\t");
                    } else {
                        impl.append("\t");
                    }
                }
            }
        });
    }

    impl.setLineWrap(true);
    impl.setWrapStyleWord(true);

    int height = (int) impl.getPreferredSize().getHeight();
    impl.setMinimumSize(new Dimension(0, height));

    composition = new JScrollPane(impl);
    composition.setPreferredSize(new Dimension(150, height));
    composition.setMinimumSize(new Dimension(0, height));

    doc.putProperty("filterNewlines", false);

    return impl;
}

From source file:net.sf.jabref.gui.maintable.MainTableSelectionListener.java

/**
 * Receive key event on the main table. If the key is a letter or a digit,
 * we should select the first entry in the table which starts with the given
 * letter in the column by which the table is sorted.
 * @param e The KeyEvent//  w  ww  .  j a  va 2  s. c o m
 */
@Override
public void keyTyped(KeyEvent e) {
    if ((!e.isActionKey()) && Character.isLetterOrDigit(e.getKeyChar()) && (e.getModifiers() == 0)) {
        long time = System.currentTimeMillis();
        final long QUICK_JUMP_TIMEOUT = 2000;
        if ((time - lastPressedTime) > QUICK_JUMP_TIMEOUT) {
            lastPressedCount = 0; // Reset last pressed character
        }
        // Update timestamp:
        lastPressedTime = time;
        // Add the new char to the search array:
        int c = e.getKeyChar();
        if (lastPressedCount < lastPressed.length) {
            lastPressed[lastPressedCount] = c;
            lastPressedCount++;
        }

        int sortingColumn = table.getSortingColumn(0);
        if (sortingColumn == -1) {
            return; // No sorting? TODO: look up by author, etc.?
        }
        // TODO: the following lookup should be done by a faster algorithm,
        // such as binary search. But the table may not be sorted properly,
        // due to marked entries, search etc., which rules out the binary search.

        for (int i = 0; i < table.getRowCount(); i++) {
            Object o = table.getValueAt(i, sortingColumn);
            if (o == null) {
                continue;
            }
            String s = o.toString().toLowerCase();
            if (s.length() >= lastPressedCount) {
                for (int j = 0; j < lastPressedCount; j++) {
                    if (s.charAt(j) != lastPressed[j]) {
                        break; // Escape the loop immediately when we find a mismatch
                    } else if (j == (lastPressedCount - 1)) {
                        // We found a match:
                        table.setRowSelectionInterval(i, i);
                        table.ensureVisible(i);
                        return;
                    }
                }
            }
        }

    } else if (e.getKeyChar() == KeyEvent.VK_ESCAPE) {
        lastPressedCount = 0;
    }
}

From source file:EventTestPane.java

/**
 * Display keyboard events.//w w  w .ja va2 s  . com
 * 
 * Note that there are three distinct types of key events, and that key
 * events are reported by key code and/or Unicode character. KEY_PRESSED and
 * KEY_RELEASED events are generated for all key strokes. KEY_TYPED events
 * are only generated when a key stroke produces a Unicode character; these
 * events do not report a key code. If isActionKey() returns true, then the
 * key event reports only a key code, because the key that was pressed or
 * released (such as a function key) has no corresponding Unicode character.
 * Key codes can be interpreted by using the many VK_ constants defined by
 * the KeyEvent class, or they can be converted to strings using the static
 * getKeyText() method as we do here.
 */
public void processKeyEvent(KeyEvent e) {
    String eventtype, modifiers, code, character;
    switch (e.getID()) {
    case KeyEvent.KEY_PRESSED:
        eventtype = "KEY_PRESSED";
        break;
    case KeyEvent.KEY_RELEASED:
        eventtype = "KEY_RELEASED";
        break;
    case KeyEvent.KEY_TYPED:
        eventtype = "KEY_TYPED";
        break;
    default:
        eventtype = "UNKNOWN";
    }

    // Convert the list of modifier keys to a string
    modifiers = KeyEvent.getKeyModifiersText(e.getModifiers());

    // Get string and numeric versions of the key code, if any.
    if (e.getID() == KeyEvent.KEY_TYPED)
        code = "";
    else
        code = "Code=" + KeyEvent.getKeyText(e.getKeyCode()) + " (" + e.getKeyCode() + ")";

    // Get string and numeric versions of the Unicode character, if any.
    if (e.isActionKey())
        character = "";
    else
        character = "Character=" + e.getKeyChar() + " (Unicode=" + ((int) e.getKeyChar()) + ")";

    // Display it all.
    showLine(eventtype + ": " + modifiers + " " + code + " " + character);
}

From source file:com.github.fritaly.dualcommander.DualCommander.java

@Override
public void keyPressed(KeyEvent e) {
    if ((e.getModifiers() | KeyEvent.SHIFT_MASK) == KeyEvent.SHIFT_MASK) {
        shiftPressed = true;/*w w  w .  j  a  va 2 s .  co m*/

        if (logger.isDebugEnabled()) {
            logger.debug("[Shift] key pressed");
        }
    }
    if ((e.getModifiers() | KeyEvent.META_MASK) == KeyEvent.META_MASK) {
        metaPressed = true;

        if (logger.isDebugEnabled()) {
            logger.debug("[Meta] key pressed");
        }
    }
}