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:org.yccheok.jstock.gui.StockDatabaseJDialog.java

private void jTable2KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTable2KeyPressed
    if (KeyEvent.VK_DELETE == evt.getKeyCode()) {
        this.deleteSelectedUserDefinedDatabase();
    }/* w  w  w  . j  av a  2s.  c  om*/
}

From source file:edu.ku.brc.af.ui.forms.validation.ValComboBox.java

/**
 * @param makeEditable/*from   w w  w .  ja v  a2 s  . c  om*/
 */
public void init(final boolean makeEditable) {
    if (makeEditable) {
        Java2sAutoComboBox cbx = (Java2sAutoComboBox) comboBox;
        textEditor = cbx.getAutoTextFieldEditor().getAutoTextFieldEditor();
        textEditor.addKeyListener(getTextKeyAdapter());
        addPopupMenu(textEditor);

        comboBox.addKeyListener(new KeyAdapter() {

            @Override
            public void keyPressed(KeyEvent e) {
                super.keyPressed(e);
                if (e.getKeyCode() == KeyEvent.VK_ESCAPE || e.getKeyCode() == KeyEvent.VK_DELETE
                        || e.getKeyCode() == KeyEvent.VK_BACK_SPACE) {
                    comboBox.setSelectedIndex(-1);
                }
                notifyChangeListeners(new ChangeEvent(ValComboBox.this));
            }

            @Override
            public void keyReleased(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_ESCAPE || e.getKeyCode() == KeyEvent.VK_DELETE
                        || e.getKeyCode() == KeyEvent.VK_BACK_SPACE) {
                    comboBox.setSelectedIndex(-1);
                }
                super.keyReleased(e);
            }

            /* (non-Javadoc)
             * @see java.awt.event.KeyAdapter#keyTyped(java.awt.event.KeyEvent)
             */
            @Override
            public void keyTyped(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_ESCAPE || e.getKeyCode() == KeyEvent.VK_DELETE
                        || e.getKeyCode() == KeyEvent.VK_BACK_SPACE) {
                    comboBox.setSelectedIndex(-1);
                }
                super.keyTyped(e);
            }

        });
    }

    setOpaque(false);

    if (defaultTextBGColor == null) {
        defaultTextBGColor = (new JTextField()).getBackground();
    }

    setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));

    PanelBuilder builder = new PanelBuilder(new FormLayout("f:p:g", "p:g:f"), this);
    CellConstraints cc = new CellConstraints();
    builder.add(comboBox, cc.xy(1, 1));

    comboBox.getModel().addListDataListener(this);

    if (valTextColor == null || requiredFieldColor == null) {
        valTextColor = AppPrefsCache.getColorWrapper("ui", "formatting", "valtextcolor");
        requiredFieldColor = AppPrefsCache.getColorWrapper("ui", "formatting", "requiredfieldcolor");
    }
    if (valTextColor != null) {
        AppPrefsCache.addChangeListener("ui.formatting.valtextcolor", this);
        AppPrefsCache.addChangeListener("ui.formatting.requiredfieldcolor", this);
    }
}

From source file:Provider.GoogleMapsStatic.TestUI.MySample.java

/**
 * @author jpmolinamatute/*from  w  w w. j  a v  a 2 s.  c om*/
 * 
 * this method control the use the longitude and latitud using the numerical  and arrows keyboard
 */
@Override
public void keyPressed(KeyEvent arg0) {
    char keyLetter = arg0.getKeyChar();
    int keyCode = arg0.getKeyCode();
    setChange();

    if (keyLetter == '8' || keyCode == 38) {
        setNorth();
        startTaskAction();

    } else if (keyLetter == '6' || keyCode == 39) {
        setEast();
        startTaskAction();

    } else if (keyLetter == '2' || keyCode == 40) {
        setSouth();
        startTaskAction();

    } else if (keyLetter == '4' || keyCode == 37) {
        setWest();
        startTaskAction();
    } else if (keyLetter == '7') {
        setNorthwest();
        startTaskAction();
    } else if (keyLetter == '9') {
        setNortheast();
        startTaskAction();
    } else if (keyLetter == '3') {
        setSoutheast();
        startTaskAction();
    } else if (keyLetter == '1') {
        setSouthwest();
        startTaskAction();
    } else if (keyLetter == 'Q' || keyLetter == 'q') {
        quitProgram();
    }

    if (keyCode == 10) {
        startTaskAction();
    }
}

From source file:ru.goodfil.catalog.ui.forms.OePanel.java

private void lstOesKeyPressed(KeyEvent e) {
    if (e != null) {
        if (e.getKeyCode() == KeyEvent.VK_INSERT) {
            btnCreateOeActionPerformed(null);
        }//  w  w w. j  a  va  2  s .  c om
        if (e.getKeyCode() == KeyEvent.VK_DELETE) {
            btnRemoveOeActionPerformed(null);
        }
        if (e.getKeyCode() == KeyEvent.VK_SPACE) {
            btnEditOeActionPerformed(null);
        }
        if ((e.getKeyCode() == KeyEvent.VK_X) && ((e.getModifiers() & KeyEvent.CTRL_MASK) != 0)) {
            List<Oe> selectedOes = oes.getSelectedItems();
            putObjectToMyClipboard(selectedOes);
            this.setOperation(OPERATION_CUT);
        }
        if ((e.getKeyCode() == KeyEvent.VK_C) && ((e.getModifiers() & KeyEvent.CTRL_MASK) != 0)) {
            List<Oe> selectedOes = oes.getSelectedItems();
            putObjectToMyClipboard(selectedOes);
            this.setOperation(OPERATION_COPY);
        }
        if ((e.getKeyCode() == KeyEvent.VK_V) && ((e.getModifiers() & KeyEvent.CTRL_MASK) != 0)) {
            if (popupMenu1.getInvoker() == lstOes) {
                List<Oe> selectedOes = new ArrayList();
                selectedOes.addAll(getFromClipboardByType(Oe.class));
                if (selectedOes.size() > 0) {
                    Long brand = brands.getSelectedItemId();
                    Assert.notNull(brand);
                    if (getOperation() == OPERATION_COPY) {
                        analogsService.doCopyOe(selectedOes, brand);
                    }
                    if (getOperation() == OPERATION_CUT) {
                        analogsService.doCutOe(selectedOes, brand);
                    }
                    reReadOes(brand);
                }
            }
        }
    }
}

From source file:AvatarTest.java

/**
 * Process a keyboard event//  w  ww . ja  v a 2 s . c o  m
 */
private void processAWTEvent(AWTEvent[] events) {
    for (int n = 0; n < events.length; n++) {
        if (events[n] instanceof KeyEvent) {
            KeyEvent eventKey = (KeyEvent) events[n];

            if (eventKey.getID() == KeyEvent.KEY_PRESSED) {
                int keyCode = eventKey.getKeyCode();
                int keyChar = eventKey.getKeyChar();

                Vector3f translate = new Vector3f();

                Transform3D t3d = new Transform3D();
                m_TransformGroup.getTransform(t3d);
                t3d.get(translate);

                switch (keyCode) {
                case KeyEvent.VK_LEFT:
                    translate.x += TRANSLATE_LEFT;
                    break;

                case KeyEvent.VK_RIGHT:
                    translate.x += TRANSLATE_RIGHT;
                    break;
                }

                // System.out.println( "Steering: " + translate.x );
                translate.y = 0.5f;

                t3d.setTranslation(translate);
                m_TransformGroup.setTransform(t3d);
            }
        }
    }
}

From source file:com.floreantpos.jasperreport.swing.JRViewerPanel.java

protected void keyNavigate(KeyEvent evt) {
    boolean refresh = true;
    switch (evt.getKeyCode()) {
    case KeyEvent.VK_DOWN:
    case KeyEvent.VK_PAGE_DOWN:
        dnNavigate(evt);// ww w .  j a  v  a 2 s  .co  m
        break;
    case KeyEvent.VK_UP:
    case KeyEvent.VK_PAGE_UP:
        upNavigate(evt);
        break;
    case KeyEvent.VK_HOME:
        homeEndNavigate(0);
        break;
    case KeyEvent.VK_END:
        homeEndNavigate(viewerContext.getPageCount() - 1);
        break;
    default:
        refresh = false;
    }

    if (refresh) {
        viewerContext.refreshPage();
    }
}

From source file:com.brainflow.application.toplevel.Brainflow.java

private void initializeToolBar() {

    CommandGroup mainToolbarGroup = new CommandGroup("main-toolbar");
    mainToolbarGroup.bind(getApplicationFrame());

    ToggleGroup interpToggleGroup = new ToggleGroup("toggle-interp-group");
    interpToggleGroup.bind(getApplicationFrame());

    OpenImageCommand openImageCommand = new OpenImageCommand();
    openImageCommand.bind(getApplicationFrame());

    SnapshotCommand snapshotCommand = new SnapshotCommand();
    snapshotCommand.bind(getApplicationFrame());

    CreateAxialViewCommand axialCommand = new CreateAxialViewCommand();
    axialCommand.bind(getApplicationFrame());
    axialCommand.installShortCut(documentPane, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

    CreateSagittalViewCommand sagittalCommand = new CreateSagittalViewCommand();
    sagittalCommand.bind(getApplicationFrame());
    sagittalCommand.installShortCut(documentPane, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

    CreateCoronalViewCommand coronalCommand = new CreateCoronalViewCommand();
    coronalCommand.bind(getApplicationFrame());
    coronalCommand.installShortCut(documentPane, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

    CreateVerticalOrthogonalCommand vertCommand = new CreateVerticalOrthogonalCommand();
    vertCommand.bind(getApplicationFrame());
    vertCommand.installShortCut(documentPane, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

    CreateHorizontalOrthogonalCommand horizCommand = new CreateHorizontalOrthogonalCommand();
    horizCommand.bind(getApplicationFrame());
    horizCommand.installShortCut(documentPane, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

    CreateTriangularOrthogonalCommand triCommand = new CreateTriangularOrthogonalCommand();
    triCommand.bind(getApplicationFrame());
    triCommand.installShortCut(documentPane, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

    CommandGroup orthoGroup = new CommandGroup("ortho-view-group");
    orthoGroup.bind(getApplicationFrame());

    final NextSliceCommand nextSliceCommand = new NextSliceCommand();
    nextSliceCommand.bind(getApplicationFrame());
    nextSliceCommand.installShortCut(documentPane, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

    final PreviousSliceCommand previousSliceCommand = new PreviousSliceCommand();
    previousSliceCommand.bind(getApplicationFrame());
    previousSliceCommand.installShortCut(documentPane, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

    Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
        public void eventDispatched(AWTEvent event) {
            if (event.getID() == KeyEvent.KEY_PRESSED) {
                KeyEvent ke = (KeyEvent) event;
                if (ke.getKeyCode() == KeyEvent.VK_LEFT) {
                    previousSliceCommand.execute();
                } else if (ke.getKeyCode() == KeyEvent.VK_RIGHT) {
                    nextSliceCommand.execute();
                }//from  w ww . j a  va  2s  . co  m

            }

        }
    }, AWTEvent.KEY_EVENT_MASK);

    PageBackSliceCommand pageBackSliceCommand = new PageBackSliceCommand();
    pageBackSliceCommand.bind(getApplicationFrame());
    pageBackSliceCommand.installShortCut(documentPane, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

    PageForwardSliceCommand pageForwardSliceCommand = new PageForwardSliceCommand();
    pageForwardSliceCommand.bind(getApplicationFrame());
    pageForwardSliceCommand.installShortCut(documentPane, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

    IncreaseContrastCommand increaseContrastCommand = new IncreaseContrastCommand();
    increaseContrastCommand.bind(getApplicationFrame());
    increaseContrastCommand.installShortCut(documentPane, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

    DecreaseContrastCommand decreaseContrastCommand = new DecreaseContrastCommand();
    decreaseContrastCommand.bind(getApplicationFrame());
    decreaseContrastCommand.installShortCut(documentPane, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

    ToggleCommand nearest = new NearestInterpolationToggleCommand();
    nearest.bind(getApplicationFrame());
    decreaseContrastCommand.installShortCut(documentPane, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

    ToggleCommand linear = new LinearInterpolationToggleCommand();
    linear.bind(getApplicationFrame());

    ToggleCommand cubic = new CubicInterpolationToggleCommand();
    cubic.bind(getApplicationFrame());

    ToggleCommand toggleAxisLabelCommand = new ToggleAxisLabelCommand();
    toggleAxisLabelCommand.bind(getApplicationFrame());

    JToolBar mainToolbar = mainToolbarGroup.createToolBar();
    //ActionCommand increaseContrastCommand = new IncreaseContrastCommand();
    //increaseContrastCommand.bind(brainFrame);
    //mainToolbar.add(increaseContrastCommand.getActionAdapter());

    //ActionCommand decreaseContrastCommand = new DecreaseContrastCommand();
    //decreaseContrastCommand.bind(brainFrame);
    //mainToolbar.add(decreaseContrastCommand.getActionAdapter());

    brainFrame.getContentPane().add(mainToolbar, BorderLayout.NORTH);

    //InputMap map = documentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);

    //for (KeyStroke ks : map.keys()) {
    //    System.out.println("key : " + ks);
    //}

}

From source file:org.ut.biolab.medsavant.client.view.component.SearchableTablePanel.java

/**
 * The given action will be executed when the selection changes AND a
 * scrolling key (pg-up, pg-down, arrow keys, num lock arrow keys) is not
 * being held down. This method should be used as a scroll-safe alternative
 * to registering a selection listener./*from   www  .  j a v a 2  s. c o m*/
 *
 * This makes it safe for the user to scroll through the scroll panel
 * without repeating the action unnecessarily. The action executes in a new
 * thread.
 */
public void scrollSafeSelectAction(final Runnable onSelectTask) {

    final KeyTimer keyTimer = new KeyTimer(KEY_PRESS_TIMER_INTERVAL, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent ae) {
            keydown = false;
            waitlong = false;
            //printTime("Starting Worker");
            resetSelectionChangedWorker(onSelectTask);

        }
    });
    keyTimer.setRepeats(false);

    getTable().addKeyListener(new KeyAdapter() {
        private long lastDelta = -1;
        private long lastTime = System.currentTimeMillis();
        private int deltaSame = 0;
        private boolean first = true;

        @Override
        public void keyPressed(KeyEvent ke) {
            super.keyPressed(ke);

            if (!ArrayUtils.contains(SCROLLING_KEYS, ke.getKeyCode())) {
                //System.out.println("Detected key press that wasn't a scrolling key! (keycode=" + ke.getKeyCode() + ")");
                return;
            }

            long currentTime = System.currentTimeMillis();
            long delta = currentTime - lastTime;

            if (Math.abs(delta - lastDelta) < KEY_PRESS_INTERVAL_EPSILON) {
                deltaSame++;
            } else {
                deltaSame = 0;
            }

            if (deltaSame > KEY_PRESS_TIMER_INTERVAL_AUTOADJUST_RUNS) {
                //If this is the first time we've detected an interval, or if the detected interval
                //is getting close to the current (padded) interval, then change the current interval to
                //the detected one + padding.
                if (first || (delta - KEY_PRESS_TIMER_INTERVAL) > (KEY_PRESS_TIMER_INTERVAL_AUTOADJUST_PADDING
                        / 2.0f)) {
                    KEY_PRESS_TIMER_INTERVAL = (int) delta + KEY_PRESS_TIMER_INTERVAL_AUTOADJUST_PADDING;
                    LOG.info("Detected " + deltaSame + " keypresses with delta ~" + delta
                            + ", setting new repeat-interval to " + KEY_PRESS_TIMER_INTERVAL);
                    first = false;
                }
                deltaSame = 0;
            }

            lastDelta = delta;
            lastTime = currentTime;

            keydown = true;
            stopSelectionChangedWorker();
            if (keyTimer.isRunning()) {
                keyTimer.stop("keyPressed stop");
                if (waitlong) {
                    waitlong = false;
                    keyTimer.setInitialDelay(KEY_PRESS_TIMER_INTERVAL_LONG);
                } else {
                    keyTimer.setInitialDelay(KEY_PRESS_TIMER_INTERVAL);
                }
                keyTimer.restart("keyPressed restart");
            } else {
                waitlong = false;
                keyTimer.setInitialDelay(KEY_PRESS_TIMER_INTERVAL_LONG);
                keyTimer.start("keyTimer start");
            }

        }
        //In Linux, holding a keydown fires pairs of keyPressed and keyReleased events
        //continually, so we cannot rely on 'keyReleased'.
        /*@Override
        public void keyReleased(KeyEvent ke) {
        super.keyReleased(ke); //To change body of generated methods, choose Tools | Templates.
        keydown = false;
        }*/
    });
    getTable().getSelectionModel().addListSelectionListener(new ListSelectionListener() {
        @Override
        public void valueChanged(ListSelectionEvent e) {
            if (e.getValueIsAdjusting()) {
                return;
            }

            if (keydown == false) {
                if (!keyTimer.isRunning()) {
                    waitlong = true;
                    // printTime("valueChanged, setting waitlong=true: ");
                    keyTimer.setInitialDelay(KEY_PRESS_TIMER_INTERVAL);
                    keyTimer.start("valueChanged start");
                }
            }
        }
    });
}

From source file:org.pdfsam.guiclient.commons.panels.JVisualPdfPageSelectionPanel.java

private void initKeyListener() {
    //key listener
    thumbnailList.addKeyListener(new KeyAdapter() {
        public void keyPressed(KeyEvent e) {
            if ((e.isAltDown()) && (e.getKeyCode() == KeyEvent.VK_UP)) {
                moveUpButton.doClick();//w w  w. j av  a  2  s .c  om
            } else if ((e.isAltDown()) && (e.getKeyCode() == KeyEvent.VK_DOWN)) {
                moveDownButton.doClick();
            } else if ((e.getKeyCode() == KeyEvent.VK_DELETE)) {
                removeButton.doClick();
            } else if (drawDeletedItems && (e.isControlDown()) && (e.getKeyCode() == KeyEvent.VK_Z)) {
                undeleteButton.doClick();
            } else if ((e.isAltDown()) && (e.getKeyCode() == KeyEvent.VK_RIGHT)) {
                rotateButton.doClick();
            } else if ((e.isAltDown()) && (e.getKeyCode() == KeyEvent.VK_LEFT)) {
                rotateAntiButton.doClick();
            }
        }
    });
}

From source file:com.frostwire.gui.player.MediaPlayer.java

protected MediaPlayer() {
    lastRandomFiles = new LinkedList<MediaSource>();
    playExecutor = ExecutorsHelper.newProcessingQueue("AudioPlayer-PlayExecutor");

    String playerPath;/*  w w w.  j av  a  2s. com*/
    playerPath = getPlayerPath();

    MPlayer.initialise(new File(playerPath));
    mplayer = new MPlayer();
    mplayer.addPositionListener(new PositionListener() {
        public void positionChanged(float currentTimeInSecs) {
            notifyProgress(currentTimeInSecs);
        }
    });
    mplayer.addStateListener(new StateListener() {
        public void stateChanged(MediaPlaybackState newState) {
            if (newState == MediaPlaybackState.Closed) { // This is the case
                                                         // mplayer is
                                                         // done with the
                                                         // current file
                playNextMedia();
            }
        }
    });
    mplayer.addIcyInfoListener(new IcyInfoListener() {
        public void newIcyInfoData(String data) {
            notifyIcyInfo(data);
        }
    });

    repeatMode = RepeatMode.values()[PlayerSettings.LOOP_PLAYLIST.getValue()];
    shuffle = PlayerSettings.SHUFFLE_PLAYLIST.getValue();
    playNextMedia = true;
    volume = PlayerSettings.PLAYER_VOLUME.getValue();
    notifyVolumeChanged();

    KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new KeyEventDispatcher() {

        @Override
        public boolean dispatchKeyEvent(KeyEvent e) {
            if (e.getID() == KeyEvent.KEY_PRESSED && e.getKeyCode() == KeyEvent.VK_SPACE) {
                Object s = e.getComponent();
                if (!(s instanceof JTextField)
                        && !(s instanceof JTable && ((JTable) s).isEditing() && !(s instanceof JCheckBox))) {
                    togglePause();
                    return true;
                }
            }
            return false;
        }
    });

    // prepare to receive UI events
    MPlayerUIEventHandler.instance().addListener(this);
}