java.awt.KeyEventDispatcher Java Examples

The following examples show how to use java.awt.KeyEventDispatcher. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SwingMain.java    From chip8 with MIT License 6 votes vote down vote up
/**
 * Creates new form SwingMain
 */
public SwingMain() {
    this.chip8Runner = new Thread(() -> {
        while (true) {
            try {
                if (running) {
                    if (pause == false || step) {
                        step = false;
                        chip8.cycle();
                        ((Chip8Model) jTable1.getModel()).update(chip8);
                    }
                    outputPanel.repaint();
                }
                sleep(1);
            } catch (InterruptedException ex) {
                Logger.getLogger(SwingMain.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    });
    initComponents();
    KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
    manager.addKeyEventDispatcher(new KeyEventDispatcher() {
        @Override
        public boolean dispatchKeyEvent(KeyEvent e) {
            switch (e.getID()) {
                case KeyEvent.KEY_PRESSED:
                    Input.press(e.getKeyChar() + "");
                    break;
                case KeyEvent.KEY_RELEASED:
                    Input.unpress();
                    break;
                default:
                    break;
            }
            return false;
        }
    });
}
 
Example #2
Source File: MainFrame.java    From netbeans-mmd-plugin with Apache License 2.0 4 votes vote down vote up
private void menuFullScreenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuFullScreenActionPerformed

    final AbstractEditor selectedEditor = this.tabPane.getCurrentEditor();
    if (selectedEditor != null) {
      final GraphicsConfiguration gconfig = this.getGraphicsConfiguration();
      if (gconfig != null) {
        final GraphicsDevice device = gconfig.getDevice();
        if (device.isFullScreenSupported()) {
          if (device.getFullScreenWindow() == null) {
            final JLabel label = new JLabel("Opened in full screen");
            final int tabIndex = this.tabPane.getSelectedIndex();
            this.tabPane.setComponentAt(tabIndex, label);
            final JWindow window = new JWindow(Main.getApplicationFrame());
            window.setAlwaysOnTop(true);
            window.setAutoRequestFocus(true);
            window.setContentPane(selectedEditor.getContainerToShow());

            endFullScreenIfActive();

            final KeyEventDispatcher fullScreenEscCatcher = (@Nonnull final KeyEvent e) -> {
              if (e.getID() == KeyEvent.KEY_PRESSED && (e.getKeyCode() == KeyEvent.VK_ESCAPE || e.getKeyCode() == KeyEvent.VK_F11)) {
                endFullScreenIfActive();
                return true;
              }
              return false;
            };

            if (this.taskToEndFullScreen.compareAndSet(null, (Runnable) () -> {
              try {
                window.dispose();
              } finally {
                tabPane.setComponentAt(tabIndex, selectedEditor.getContainerToShow());
                device.setFullScreenWindow(null);
                KeyboardFocusManager.getCurrentKeyboardFocusManager().removeKeyEventDispatcher(fullScreenEscCatcher);
              }
            })) {
              try {
                KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(fullScreenEscCatcher);
                device.setFullScreenWindow(window);
              } catch (Exception ex) {
                LOGGER.error("Can't turn on full screen", ex); //NOI18N
                endFullScreenIfActive();
                KeyboardFocusManager.getCurrentKeyboardFocusManager().removeKeyEventDispatcher(fullScreenEscCatcher);
              }
            } else {
              LOGGER.error("Unexpected state, processor is not null!"); //NOI18N
            }
          } else {
            LOGGER.warn("Attempt to full screen device which already in full screen!"); //NOI18N
          }
        } else {
          LOGGER.warn("Device doesn's support full screen"); //NOI18N
          DialogProviderManager.getInstance().getDialogProvider().msgWarn(this, "The Device doesn't support full-screen mode!");
        }
      } else {
        LOGGER.warn("Can't find graphics config for the frame"); //NOI18N
      }
    }
  }
 
Example #3
Source File: frmControl.java    From sourcerabbit-gcode-sender with GNU General Public License v2.0 4 votes vote down vote up
private void InitKeyListener()
{
    KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new KeyEventDispatcher()
    {
        @Override
        public boolean dispatchKeyEvent(KeyEvent e)
        {
            // Check if a "text" option is focused
            boolean textIsFocused = jTextFieldGCodeFile.hasFocus()
                    || jTextFieldCommand.hasFocus()
                    || (e.getSource() instanceof JFormattedTextField
                    || jTextAreaConsole.hasFocus()
                    || jTableGCodeLog.hasFocus()
                    || fMacroTexts.contains(e.getSource())
                    || fMacroButtons.contains(e.getSource()));

            if (!textIsFocused && jCheckBoxEnableKeyboardJogging.isSelected() && e.getID() == KeyEvent.KEY_PRESSED)
            {
                boolean jog = false;
                final String jogAxis;

                switch (e.getKeyCode())
                {
                    case KeyEvent.VK_RIGHT:
                    case KeyEvent.VK_KP_RIGHT:
                        jog = true;
                        jogAxis = "X";
                        break;

                    case KeyEvent.VK_LEFT:
                    case KeyEvent.VK_KP_LEFT:
                        jog = true;
                        jogAxis = "X-";
                        break;

                    case KeyEvent.VK_UP:
                    case KeyEvent.VK_KP_UP:
                        jog = true;
                        jogAxis = "Y";
                        break;

                    case KeyEvent.VK_DOWN:
                    case KeyEvent.VK_KP_DOWN:
                        jog = true;
                        jogAxis = "Y-";
                        break;

                    case KeyEvent.VK_PAGE_UP:
                        jog = true;
                        jogAxis = "Z";
                        break;

                    case KeyEvent.VK_PAGE_DOWN:
                        jog = true;
                        jogAxis = "Z-";
                        break;

                    default:
                        jogAxis = "";
                        break;
                }

                if (jog)
                {
                    final double stepValue = (double) jSpinnerStep.getValue();

                    Thread th = new Thread(() ->
                    {
                        Process_Jogging p = new Process_Jogging(null, jogAxis, stepValue, fJoggingUnits);
                        p.Execute();
                        p.Dispose();
                    });
                    th.start();

                    return true;
                }
            }

            return false;
        }

    });
}