Jump to content
  • Advertisement
Sign in to follow this  
Dark Star

[java] Java console programs: receiving input from keyboard..?

This topic is 4861 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi guys and girls, I just wanted to know if there is a way you can receive input from the keyboard in a java console program with out it waiting for you to have to press enter. This is because Im running a simulation program that has to keep running and displaying, but I want to the user be able to adjust features of the sim without by pressing keys while the program runs. e.g.
while (true)
{
   runSim();
   displayOutput();

   char keyChar = getKey();  // this method would have java code to get input but not wait for an enter.

   // respond to key character
}
  

I know u can do this C++ console programs using functions in stdio I think. I honestly hope there is a way in Java :) Thanks for any help. Easy! DarkStar UK

Share this post


Link to post
Share on other sites
Advertisement
You can use the avaliable method of the InputStream class to determine if there is anything to read, like so:


if ( System.in.avaliable() > 0 )
dostuffwithinput();

Share this post


Link to post
Share on other sites
Thanks for the help you guys.

readLine() in BufferedReader class doesn't appear to allow me to read character without having to press enter.

I dont want to use a thread because this assignment doesnt call for it.

Luctus, the available function will tell me how many bytes are ready to be read, but what method would i use to get the data, without my program blocking or without it waiting for me to press enter.

Thanks

DarkStar
U.K.

Share this post


Link to post
Share on other sites
A call to System.in.read() should do the trick.

while (true)
{
runSim();
displayOutput();

if ( System.in.avaliable() > 0 )
{
char keyChar = (char)System.in.read();

// respond to key character
}
}

Share this post


Link to post
Share on other sites
Hey Luctus,

I tried your last approach identically and it didnt work at all. Any reason or other ideas?


Thanks

DarkStar
UK

Share this post


Link to post
Share on other sites
you'll have to fake it:

import java.awt.Color;
import java.awt.Font;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;
import javax.swing.JTextPane;
/*
* Created on Jul 24, 2005
*
*/


public class MainFrame extends JFrame implements KeyListener
{
JTextPane j;
public MainFrame ()
{
super ("title");
setBounds (0, 0, 640, 480);
setDefaultCloseOperation (EXIT_ON_CLOSE);
j = new JTextPane ();
j.setBackground(Color.BLACK);
j.setForeground(Color.LIGHT_GRAY);
j.setFont(new Font("courier", Font.BOLD, 15));
j.addKeyListener (this);
getContentPane ().add (j);
setVisible (true);
}
public void keyPressed (KeyEvent arg0)
{
// skip it, just need to implement it

}

public void keyReleased (KeyEvent arg0)
{
// skip it, just need to implement it
}

public void keyTyped (KeyEvent arg0)
{
char c = arg0.getKeyChar ();
// do your stuff;
print ((char) (c + 1) + " ");
}
private void print (String s)
{
j.setText (j.getText () + s);
}
private void println (String s)
{
j.setText (j.getText () + s + "\n");
}
/**
* @param args
*/

public static void main (String[] args)
{
new MainFrame ();

}

}


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!