1

I am creating a simple console application in which I can use keyboard arrow keys as an input like a typical remote of a toy. When I press arrow up the console will print the output text "UP" or if I press arrow down it will print "down".

I want to press the arrow key only once, i.e. I am not needed to press enter afterwards to accept my input. I want the input to be accepted automatically on pressing the arrow key.

I already tried some code but this is still not happening and I still need to press enter to accept my input. If you have any idea how I can achieve this as simple as possible, I would really appreciate it.

Raul Rene
  • 10,014
  • 9
  • 53
  • 75
Zyrax
  • 171
  • 2
  • 7
  • 16
  • Take a look on this question. http://stackoverflow.com/questions/9545388/how-can-i-detect-arrow-keys-in-java-console-not-in-gui – AlexR Aug 23 '12 at 09:06
  • You said you have already tried something. Please post code snippets and also you could try posting what you have tried. – Raul Rene Aug 23 '12 at 09:07

2 Answers2

1

This sample code will helps you to get the Left Arrow Key Event. You can refer this,

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.*;

public class Test2 extends JPanel {
   private static final int PREF_W = 400;
   private static final int PREF_H = PREF_W;
   private static final int TIMER_DELAY = 50;
   private Timer leftKeyTimer = new Timer(TIMER_DELAY , new TimerListener());


   public Test2() {
      int condition = JComponent.WHEN_IN_FOCUSED_WINDOW;
      InputMap inputMap = getInputMap(condition );
      ActionMap actionMap = getActionMap();

      String leftDownKey = "Left Down";
      String leftUpKey = "Left Up";
      KeyStroke leftDown = KeyStroke.getKeyStroke(KeyEvent.VK_LEFT , 0, false);
      KeyStroke leftUp = KeyStroke.getKeyStroke(KeyEvent.VK_LEFT , 0, true);
      inputMap.put(leftDown, leftDownKey);
      inputMap.put(leftUp, leftUpKey);

      actionMap.put(leftDownKey, new LeftKeyAction(false));
      actionMap.put(leftUpKey, new LeftKeyAction(true));
      leftKeyTimer.setActionCommand("Left Key");
   }

   @Override
   public Dimension getPreferredSize() {
      return new Dimension(PREF_W, PREF_H);
   }

   private class LeftKeyAction extends AbstractAction {
      private boolean onKeyRelease;

      public LeftKeyAction(boolean onKeyRelease) {
         this.onKeyRelease = onKeyRelease;
      }

      @Override
      public void actionPerformed(ActionEvent e) {
         if (onKeyRelease) {
            if (leftKeyTimer != null && leftKeyTimer.isRunning()) {
               leftKeyTimer.stop();
            }
         } else {
            if (leftKeyTimer != null && !leftKeyTimer.isRunning()) {
               leftKeyTimer.start();
            }

         }
      }
   }

   private class TimerListener implements ActionListener {
      public void actionPerformed(ActionEvent actEvt) {
         System.out.println(actEvt.getActionCommand());
      }
   }

   private static void createAndShowGui() {
      Test2 mainPanel = new Test2();

      JFrame frame = new JFrame("Test2");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}
Vinesh
  • 933
  • 2
  • 7
  • 22
  • Thank you for this cut-and-paste code example. I have been able to modify this to my needs. I appreciate the completeness of your code (not everyone includes the import section!). – efelton Aug 17 '18 at 00:46
0

This is actually a surprisingly complicated problem. If you want a true console app (no GUI elements) you have to sacrifice portability.

Most consoles are line buffered by default, so Java won't get any input until enter is pressed. Most can be switched to a character mode, but there is no OS independent way to do this. For more information see http://www.darkcoding.net/software/non-blocking-console-io-is-not-possible/

Vegard
  • 4,802
  • 1
  • 20
  • 30