Example usage for java.awt.event KeyEvent getKeyCode

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

Introduction

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

Prototype

public int getKeyCode() 

Source Link

Document

Returns the integer keyCode associated with the key in this event.

Usage

From source file:edu.mit.fss.tutorial.part4.ControlPanel.java

/**
 * Instantiates a new control panel./*w  w w  .jav a 2  s .com*/
 */
public ControlPanel() {
    setFocusable(true); // required for key listener
    setPreferredSize(new Dimension(300, 300));
    setBackground(Color.white);

    // Add a new KeyAdapter object instance to bind actions to key presses.
    addKeyListener(new KeyAdapter() {
        @Override
        public void keyPressed(KeyEvent e) {
            if (boundElement == null) {
                // Do nothing if no element is bound.
                return;
            }
            if (e.getKeyCode() == KeyEvent.VK_UP) {
                // Increment Y-velocity.
                boundElement.setVelocity(boundElement.getVelocity().add(new Vector3D(0, 1, 0)));
            } else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
                // Decrement Y-velocity.
                boundElement.setVelocity(boundElement.getVelocity().add(new Vector3D(0, -1, 0)));
            } else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
                // Decrement X-velocity.
                boundElement.setVelocity(boundElement.getVelocity().add(new Vector3D(-1, 0, 0)));
            } else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
                // Increment X-velocity.
                boundElement.setVelocity(boundElement.getVelocity().add(new Vector3D(1, 0, 0)));
            }
        }
    });
}

From source file:com.willwinder.universalgcodesender.uielements.components.CommandTextArea.java

/**
 * The up/down keyboard events cycle through previous commands.
 *///from www.  ja  v a  2 s.com
@Override
public boolean dispatchKeyEvent(KeyEvent e) {
    if (e.getID() == KeyEvent.KEY_PRESSED && commandHistory.size() > 0 && (this.hasFocus() || focusNotNeeded)
            && isArrowKey(e)) {
        boolean pressed = false;

        if (e.getKeyCode() == KeyEvent.VK_UP) {
            pressed = true;
            commandNum++;
        } else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
            pressed = true;
            commandNum--;
        }

        if (pressed) {
            if (commandNum < 0) {
                commandNum = -1;
                setText("");
                java.awt.Toolkit.getDefaultToolkit().beep();
                return true;
            } else if (commandNum > (commandHistory.size() - 1)) {
                commandNum = commandHistory.size() - 1;
                java.awt.Toolkit.getDefaultToolkit().beep();
            }

            // Get index from end.
            int index = commandHistory.size() - 1 - commandNum;
            String text = this.commandHistory.get(index);
            setText(text);
        }

        return true;
    }
    return false;
}

From source file:org.openmicroscopy.shoola.agents.imviewer.util.player.MoviePlayerControl.java

/** Adds listeners to the UI components. */
private void initListeners() {
    JTextField editor = view.editor;
    editor.addKeyListener(new KeyAdapter() {

        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_ENTER)
                editorActionHandler();/*from   w w w  .  j a va  2s .co m*/
        }
    });
    //JButton
    attachButtonListener(view.play, PLAY_CMD);
    attachButtonListener(view.pause, PAUSE_CMD);
    attachButtonListener(view.stop, STOP_CMD);
    //JComboBox
    JComboBox box = view.movieTypes;
    box.setActionCommand("" + MOVIE_TYPE_CMD);
    box.addActionListener(this);
    //JSpinner
    view.fps.addChangeListener(this);
    //MoviePane
    attachFieldListeners(view.startT);
    attachFieldListeners(view.endT);
    attachFieldListeners(view.startZ);
    attachFieldListeners(view.endZ);
    attachButtonListener(view.acrossZ, ACROSS_Z_CMD);
    attachButtonListener(view.acrossT, ACROSS_T_CMD);
    //attachButtonListener(view.acrossZT, ACROSS_ZT_CMD);
    view.tSlider.addPropertyChangeListener(this);
    view.zSlider.addPropertyChangeListener(this);
}

From source file:com.nvinayshetty.DTOnator.Ui.InputWindow.java

private void initListeners() {
    inputFeedText.addMouseListener(new ContextMenuMouseListener());
    inputFeedText.addKeyListener(new KeyListener() {
        @Override/*from   w w w .j av a  2s .  c om*/
        public void keyTyped(KeyEvent e) {
        }

        @Override
        public void keyPressed(KeyEvent e) {
            switch (e.getKeyCode()) {
            case KeyEvent.VK_ENTER:
                onOK();
                break;
            case KeyEvent.VK_ESCAPE:
                onCancel();
                break;
            }
        }

        @Override
        public void keyReleased(KeyEvent e) {
        }
    });
    makeFieldsPrivate.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (makeFieldsPrivate.isSelected()) {
                setEncapsulationOptionsVisible(true);
                SetEncapsulationOptionsSelected(true);
            } else {
                setEncapsulationOptionsVisible(false);
            }
        }
    });
}

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()));
    }//  ww w  .j a va  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  .ja  v a 2 s . c o  m
}

From source file:de.wusel.partyplayer.gui.PartyPlayer.java

private JComponent createMainComponent() {
    JPanel mainPanel = new JPanel(new MigLayout("fill", "[][50%][][50%]", "[] [] [] [] [grow]"));
    mainPanel.add(new JLabel(getText("layout.current.title")));
    mainPanel.add(new JSeparator(), "growx");
    mainPanel.add(new JLabel(getText("layout.next.title")));
    mainPanel.add(new JSeparator(), "growx, wrap");
    mainPanel.add(createPlayerPanel(), "grow, span 2");
    mainPanel.add(createPlayListPanel(), "grow, hmax 100, span 2, wrap");
    mainPanel.add(new JLabel(getText("layout.available.title")));
    mainPanel.add(new JSeparator(), "growx, span, wrap");
    final JTextField searchField = new JTextField();
    searchField.addKeyListener(new KeyAdapter() {

        @Override//from www.j  a v a2 s.c  om
        public void keyReleased(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
                searchField.setText(null);
                table.setRowFilter(null);
            }
            try {
                table.setRowFilter(
                        RowFilter.regexFilter("(?i)" + Pattern.quote(searchField.getText()), 0, 1, 3));
            } catch (PatternSyntaxException ex) {
                table.setRowFilter(null);
                //do nothing
            }
        }
    });
    mainPanel.add(new JLabel(getText("layout.search.label")));
    mainPanel.add(searchField, "span, growx, wrap");
    mainPanel.add(createSongPanel(), "span, grow");
    return mainPanel;
}

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

@Override
public void keyPressed(KeyEvent e) {
    if (e.getSource() == getSelectedComponent()) {
        final boolean metaDown = (e.getModifiersEx() | KeyEvent.META_DOWN_MASK) == KeyEvent.META_DOWN_MASK;

        if ((e.getKeyCode() == KeyEvent.VK_T) && metaDown) {
            // Create a new tab and set to focus on it
            setSelectedComponent(addBrowserTab(getActiveBrowser().getDirectory()));
        } else if ((e.getKeyCode() == KeyEvent.VK_W) && metaDown) {
            if (getTabCount() > 1) {
                // Close the current tab (only if not the last one)
                closeActiveBrowserTab();
            }/* w ww  .  j a v a2s .  co  m*/
        } else if ((e.getKeyCode() >= KeyEvent.VK_1) && (e.getKeyCode() <= KeyEvent.VK_9) && metaDown) {
            final int index = e.getKeyCode() - KeyEvent.VK_1;

            if (index <= getTabCount() - 1) {
                setSelectedIndex(index);
            }
        } else {
            // Propagate event to our listeners
            processKeyEvent(new KeyEvent(this, e.getID(), e.getWhen(), e.getModifiers(), e.getKeyCode(),
                    e.getKeyChar(), e.getKeyLocation()));
        }
    }
}

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/*from w  w  w. j  a v  a  2s .  c o 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.profiler4j.console.ClassListPanel.java

/**
 * This method initializes filterTextField
 * /*from   w w  w  .  j  ava2s  .c  o  m*/
 * @return javax.swing.JTextField
 */
private JTextField getFilterTextField() {
    if (filterTextField == null) {
        filterTextField = new JTextField();
        filterTextField.setColumns(20);
        filterTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                    refreshClassList();
                }
            }
        });
    }
    return filterTextField;
}