0

Possible Duplicate:
How to read a single char from the console in Java (as the user types it)?

Im making a rogu like for the console, but is it possible to get keystrokes as they are typed?
*edit i found JLine, can it do the job? and if so how?

Community
  • 1
  • 1
TheBreadCat
  • 197
  • 1
  • 4
  • 11

1 Answers1

0

No, you can't get keypresses from the console. Because the console is dependent on the system and changes from environment to environment and from run to run, you might have one console written in Java, one written in Perl, and one written in C++, depending on where you open the program. You might also have an invisible console (you double click the JAR file).

Fortunately, you can write your own console. Doing so is fairly simple.

Here's a sample one I wrote recently:

import java.awt.BorderLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;


public class Input extends JFrame implements KeyListener{
    private JTextArea out;
    private JTextField in;
    public Input(){
        out = new JTextArea();
        in = new JTextField();
        this.add(out, BorderLayout.CENTER);
        this.add(in, BorderLayout.SOUTH);
        in.addKeyListener(this);
    }
    public void keyPressed(KeyEvent e) {
    }
    public void keyReleased(KeyEvent e) {
    }
    public void keyTyped(KeyEvent e) {
        if(e.getKeyChar() == (int)'\n'){
            e.consume();
            process(in.getText());
            in.setText("");
        }
    }

    public void process(String s){
        String text = out.getText();
        out.setText(text + ((text.length() == 0) ? "" : "\n") + s);
        String query = s.substring(0, s.indexOf(' '));
        String param = s.substring(s.indexOf(' ') + 1);
        ArrayList<String> args = new ArrayList<String>();
        int i = 0;
        boolean inQuotes = false;
        StringBuffer activeString = new StringBuffer();
        while(i < param.length()){
            if(param.charAt(i) == ' ' && !inQuotes){
                args.add(activeString.toString());
                System.out.println(activeString.toString());
                activeString.setLength(0); //FIXME:works???
            } else if(param.charAt(i) == '"'){
                inQuotes = !inQuotes;
            } else {
                activeString.append(param.charAt(i));
            }
            i++;
        }
        args.add(activeString.toString());
        System.out.println(activeString.toString());
        activeString.setLength(0); //FIXME:works???
        for (String string : args) {
            activeString.append(string + ", ");
        }
        out.setText(out.getText() + "\n\t" + "Query: " + query + "; args: " + activeString.toString());
    }

    public static void main(String[] args){
        Input x = new Input();
        x.setVisible(true);
        x.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
}

Basically, make a text field at BorderLayout.SOUTH, a text area at BorderLayout.CENTER and add a KeyListener to the text field. Then you will be able to read keypresses from the console. You'll also be able to launch the application without a console (e.g. put it in a JAR file and open it by clicking it)

TL;DR: No, but you can make your own and use that :D

Good luck!

Ryan Amos
  • 5,422
  • 4
  • 36
  • 56
  • The console there has a bunch of other junk because I'm going to use it for another application. You should remove the debugging stuff and maybe add some different methods that will suit you better. – Ryan Amos Sep 02 '11 at 18:23
  • Why the -2? What did I do wrong? – Ryan Amos Nov 24 '12 at 03:00