Detecting and acting on keyboard direction keys in Java
Asked Answered
A

4

12

G'day all,

I have a console project where it is intended the user presses the keyboard direction keys (non-numeric keypad) to move an avatar. I am having difficulty coding to check for the press of these keys. In Pascal it was easy enough to use "readkey" and code, for example, for #80 for the down keypress. However, I am stumped how to implement the same functionality in Java, though I think I understand the use of System.in and BufferedInputStream.

Could anyone help me out? Your thoughts or hints are much appreciated.

Alcantar answered 21/2, 2009 at 2:42 Comment(0)
P
5

The Console support issue in Java is well known, I am not sure that this is doable.

This was not initially possible with System.in since it used to work line-based.

Sun eventually added a java.io.Console class.

Here are its JavaDocs: http://java.sun.com/javase/6/docs/api/java/io/Console.html

Once you get the console (I think from System.console), you can get a reader and perhaps read characters from it, but I'm not sure if it includes keys.

Generally, you're supposed to use Swing or AWT if you want access to the keyboard, which is silly.

As of 2007, there was a feature request about it: here

Plug answered 21/2, 2009 at 2:50 Comment(3)
Thanks very much. I might return to Pascal then, or try and implement my project in Swing.Alcantar
Don't go back to Pascal. Java has many benefits. Console applications use the console primarily for line-based output and input, they weren't meant for full interaction, that was an aberration of DOS that is still supported in Pascal.Plug
Since most chances are that your program runs within a GUI OS, you can use swing and use a canvas that just displays text like a console if you want to do text based work.Plug
P
6

If java.io.console doesn't work for you (I haven't tried that), try JLine. I used it to solve a vaguely similar problem.

Parlay answered 21/2, 2009 at 18:38 Comment(1)
JLine is definitely the way to go, still now!Harms
P
5

The Console support issue in Java is well known, I am not sure that this is doable.

This was not initially possible with System.in since it used to work line-based.

Sun eventually added a java.io.Console class.

Here are its JavaDocs: http://java.sun.com/javase/6/docs/api/java/io/Console.html

Once you get the console (I think from System.console), you can get a reader and perhaps read characters from it, but I'm not sure if it includes keys.

Generally, you're supposed to use Swing or AWT if you want access to the keyboard, which is silly.

As of 2007, there was a feature request about it: here

Plug answered 21/2, 2009 at 2:50 Comment(3)
Thanks very much. I might return to Pascal then, or try and implement my project in Swing.Alcantar
Don't go back to Pascal. Java has many benefits. Console applications use the console primarily for line-based output and input, they weren't meant for full interaction, that was an aberration of DOS that is still supported in Pascal.Plug
Since most chances are that your program runs within a GUI OS, you can use swing and use a canvas that just displays text like a console if you want to do text based work.Plug
T
5

Unfortunately this is not possible in a portable way:

http://forums.sun.com/thread.jspa?threadID=5351637&messageID=10526512

On Windows, reading from System.in will block until enter is pressed, even when you do not use a BufferedReader. Arrows will cycle through the command history. Try it yourself:

import java.io.*;
public class KeyTest {
  public static void main(String[] argv) {
    try {
      InputStreamReader unbuffered = new InputStreamReader(System.in);
      for (int i = 0; i < 10; ++i) {
        int x = unbuffered.read();
        System.out.println(String.format("%08x", x));
      }
    } catch (Exception e) {
      System.err.println(e);
    }
  }
}

Same issue using the Console class (input buffered under Windows, arrow keys intepreted by Windows):

import java.io.*;
public class KeyTest2 {
  public static void main(String[] argv) {
    try {
      Console cons = System.console();
      if (cons != null) {
        Reader unbuffered = cons.reader();
        for (int i = 0; i < 10; ++i ) {
          int x = unbuffered.read();
          System.out.println(String.format("%08x", x));
        }
      }
    } catch (Exception e) {
      System.err.println(e);
    }
  }
}
Tye answered 21/2, 2009 at 2:57 Comment(0)
C
0

Not with built-in Java code. Check out java curses libraries or JLine as mentioned above, if you want to continue.

Clayborne answered 24/7, 2018 at 1:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.