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.

2
  • Take a look on this question. stackoverflow.com/questions/9545388/… Commented Aug 23, 2012 at 9:06
  • You said you have already tried something. Please post code snippets and also you could try posting what you have tried. Commented Aug 23, 2012 at 9:07

2 Answers 2

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();
         }
      });
   }
}
Sign up to request clarification or add additional context in comments.

1 Comment

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!).
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/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.