Example usage for java.awt KeyboardFocusManager getCurrentKeyboardFocusManager

List of usage examples for java.awt KeyboardFocusManager getCurrentKeyboardFocusManager

Introduction

In this page you can find the example usage for java.awt KeyboardFocusManager getCurrentKeyboardFocusManager.

Prototype

public static KeyboardFocusManager getCurrentKeyboardFocusManager() 

Source Link

Document

Returns the current KeyboardFocusManager instance for the calling thread's context.

Usage

From source file:com.willwinder.universalgcodesender.MainWindow.java

private void initProgram() {
    Localization.initialize(this.settings.getLanguage());
    try {/*from  w  w  w  .java 2 s  .co  m*/
        backend.applySettings(settings);
    } catch (Exception e) {
        displayErrorDialog(e.getMessage());
    }

    this.setLocalLabels();
    this.loadPortSelector();
    this.checkScrollWindow();
    this.loadFirmwareSelector();
    this.setTitle(
            Localization.getString("title") + " (" + Localization.getString("version") + " " + VERSION + ")");

    // Command History
    this.manualCommandHistory = new ArrayList<>();

    // Add keyboard listener for manual controls.
    KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new KeyEventDispatcher() {
        @Override
        public boolean dispatchKeyEvent(KeyEvent e) {
            // Check context.
            if (((arrowMovementEnabled.isSelected()) && e.getID() == KeyEvent.KEY_PRESSED)
                    && xPlusButton.isEnabled()) {
                switch (e.getKeyCode()) {
                case KeyEvent.VK_RIGHT:
                case KeyEvent.VK_KP_RIGHT:
                case KeyEvent.VK_NUMPAD6:
                    xPlusButtonActionPerformed(null);
                    e.consume();
                    return true;
                case KeyEvent.VK_LEFT:
                case KeyEvent.VK_KP_LEFT:
                case KeyEvent.VK_NUMPAD4:
                    xMinusButtonActionPerformed(null);
                    e.consume();
                    return true;
                case KeyEvent.VK_UP:
                case KeyEvent.VK_KP_UP:
                case KeyEvent.VK_NUMPAD8:
                    yPlusButtonActionPerformed(null);
                    e.consume();
                    return true;
                case KeyEvent.VK_DOWN:
                case KeyEvent.VK_KP_DOWN:
                case KeyEvent.VK_NUMPAD2:
                    yMinusButtonActionPerformed(null);
                    e.consume();
                    return true;
                case KeyEvent.VK_PAGE_UP:
                case KeyEvent.VK_NUMPAD9:
                    zPlusButtonActionPerformed(null);
                    e.consume();
                    return true;
                case KeyEvent.VK_PAGE_DOWN:
                case KeyEvent.VK_NUMPAD3:
                    zMinusButtonActionPerformed(null);
                    e.consume();
                    return true;
                case KeyEvent.VK_ADD:
                    increaseStepActionPerformed(null);
                    e.consume();
                    return true;
                case KeyEvent.VK_SUBTRACT:
                    decreaseStepActionPerformed(null);
                    e.consume();
                    return true;
                case KeyEvent.VK_DIVIDE:
                    divideStepActionPerformed(null);
                    e.consume();
                    return true;
                case KeyEvent.VK_MULTIPLY:
                    multiplyStepActionPerformed(null);
                    e.consume();
                    return true;
                case KeyEvent.VK_INSERT:
                case KeyEvent.VK_NUMPAD0:
                    resetCoordinatesButtonActionPerformed(null);
                    e.consume();
                    return true;
                default:
                    break;
                }
            }

            return false;
        }
    });
}

From source file:com.xyphos.vmtgen.GUI.java

public static void main(String args[]) {
    try {/*from w  ww . ja v  a  2 s  .c  om*/
        javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getCrossPlatformLookAndFeelClassName());

    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException
            | UnsupportedLookAndFeelException ex) {
        logger.log(Level.SEVERE, null, ex);
    }

    /*
     Create and display the form
     */ java.awt.EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            GUI gui = new GUI();
            gui.pack();

            // Global keyboard hook
            KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(gui);

            gui.setVisible(true);
        }
    });
}

From source file:com.osparking.osparking.Settings_System.java

private void makeEnterActAsTab() {
    KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
    KeyStroke tab = KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0);
    KeyStroke ctrlTab = KeyStroke.getKeyStroke(KeyEvent.VK_TAB, KeyEvent.CTRL_DOWN_MASK);
    Set<KeyStroke> keys = new HashSet<>();
    keys.add(enter);//from  w w w.j  av  a 2 s.c  om
    keys.add(tab);
    keys.add(ctrlTab);
    KeyboardFocusManager.getCurrentKeyboardFocusManager()
            .setDefaultFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, keys);
}