Console Java Program: Keyboard I/O Question

Ok,
I'm programming a cute Maze game in Java, you see. Now, I am coding the class that will take input from the user and feed it to my "game engine"..heeh
I'm trying to use the InputStreamReader class to take input from console (keyboard).
Problem is, InputStreamReader won't "take" input unless the player hits the enter key to complete the input. However, I don't want the player to have to hit enter, imagine playing Quake where instead of just hitting w to move forward, you've got to hit w, and then enter, and then w, and then enter, etc.
Is there a class, method, or way to do keyboard input so that my program will just read the next key pressed , no matter what it is, and use it as input?
Here's some of my code:
import java.util.*;
import java.io.*;
import java.lang.*;
class MannyMaze
public static void main(String[] args) throws Exception
//code needed for keyboard input
InputStreamReader console = new InputStreamReader(
new DataInputStream(System.in));
int c;
for(int i = 0; i < 10; i++)
System.out.print("input something: ");
c = console.read();
System.out.println("You inputted: " + c);
Now I'd like this to prompt the user to input something, and then "take" it as input on the next key pressed, without pressing enter.
However, as it is now, the user has to press enter after input to get it to do something.
Help!
avataR
http://plaza.ufl.edu/avatar38

Did you try a KeyListener?
They're designed to work in GUI-Apps, but perhaps they
will work with console-progs too.To add a key listener he would have to add a Component to a console prog. I don't think that would work because the program won't know its client area to listen for keystrokes like it does with a jframe for instance. But that brings up the question as to why wouldn't you write this program in a swing environment that would easily provide you with mouse and key listeners to do just what you want.

Similar Messages

  • Java programming language main method question?

    Hello everyone I am quite new to the Java programming language and I have a question here concerning my main method. As you can see I am calling 4 others methods with my main method. What does the null mean after I call the method? I really don't understand is significance, what else could go there besides null?
    public static void main(String[] args)
              int cansPerPack = 6;
              System.out.println(cansPerPack);
              int cansPerCrate = 4* cansPerPack;
              System.out.println(cansPerCrate);
              have_fun(null);
              user_input(null);
              more_java(null);
              string_work(null);
         }Edited by: phantomswordsmen on Jul 25, 2010 4:29 PM

    phantomswordsmen wrote:
    ..As you can see I am calling 4 others methods with my main method. 'Your' main method? Your questions indicate that you did not write the code, who did?
    ..What does the null mean after I call the method?.. 'null' is being passed as an argument to the method, so there is no 'after the method' about it.
    ..I really don't understand is significance, what else could go there besides null? That would depend on the method signatures that are not shown in the code snippet posted. This is one of many reasons that I recommend people to post an SSCCE *(<- link).*
    BTW - method names like have_fun() do not follow the common nomenclature, and are not good code for a newbie to study. The code should be put to the pointy end of your sword.

  • Linking java program to microsoft outlook

    how can i call outlook program from my java program??

    This question is one of the most frequenly asked questions.
    Search the forum from "Outlook" and you'll find plenty of replies.

  • Can java program simulate keyboard input?

    is it possible to create a java program or applet that simulates an end-user's keyboard input into another java applet? is it also possible to feed the output of that target applet to a file based on the different input combinations?
    and How?
    Thanks.

    Take a look at the API-docs for the class java.awt.Robot . This generates system-level input-events. Might not work in an applet, I do not know whether an applet has the right to do this.
    The second question is actually the more difficult one, since normal applets can not do file access.

  • Opening an additional console in a Java program

    Is it possible to open an additional console to be used similarly to
    System.in/out from a Java program?
    How?
    Thanks for any help!

    You can create InputStream(System.in) and PrintStream(System.out/err).
    regards,

  • Java Programming Language questions...???

    Hi everybody....
    Can I post here my questions about Java Programming Language....or only to to the relevant Sun's forum....http://forum.java.sun.com/index.jspa???
    My greetings,
    Simon

    Simon,
    sure, the worst thing that could happen is that people point you to the SUN forum. Usually this forum answers general Java questions.
    Frank

  • Java Programming Competition Sample Questions

    Hi,
    I am interested in participating the Java Programming Competition in my University. For pratice can you point me to some website with sample questions or any question u have in mind.
    Thanks
    Satish

    I was thinking of putting up on-forum competitions, winner gets dukes etc.
    Someone find an empty forum and we can get on it.

  • Java Programming Questions

    I have taken a couple class in Java programming in just regular programming with GUI/console. I am looking to further my programming skills and wonder what course I need to take to become a java programmer for companies. I am confused on all this language in Java and there does not seem to be any reference to explain exactly what these technologies are. Example is.....What is J2EE? What is .JSP? Are they the same things? What environment is most commonly used in jobs? What about Java certifications?

    Here's the path tree to become a Java Programmer from Sun Microsystems:
    http://education.oracle.com/pls/web_prod-plq-dad/db_pages.getlppage?page_id=212&path=SJPF
    There are other paths like Web Development and Databasing, so click around if you're interested in those. I think for a standard Java Programming job you will want the "Java Programming for Professionals - Sun Certified Java Programmer".
    However, most Jobs are specialized and require skills like JSP Development or Java Enterprise. Look around at a job search site and see what types of skills Java jobs are asking for.
    I've noticed, for example, that Java web jobs generally require: PHP, HTML, JSP, XML, SQL, etc.

  • Java Programming Question

    Hi,Everybody:
    I have a question about Java programming. If I want to write a java class code called if.java like below, how should I do to make it pass the java compiler and make it work. I know the words "if" and "for" are reserved words. I just try to figure it out that is it possible??? Do I need to create my own java compiler or recompiler?
    public class if
    public if() { }
    public static for test()
    return new for();
    public class for
    public for() { System.out.println("for class constructor"); }
    }

    I don't think you can bypass the compiler's rejection of this. There is little point to naming the classes after keywords. Not sure if it's case sensitive though, so maybe "If" or "For"? Otherwise, try "if_" or "for_"

  • How can I stop running java program in console ?

    Hi , my name is Luck , I will explain :
    - I have created java script which execute commands from cmd level
    - In java script I'm starting *. bat file which retrieve information from system example : hostname > C:\test and then I'd like to load from the file C:\test this host name example : MyComputer
    but the problem is that my script execute reading from the file C:\test before any information will be delivered to this file , how can make a system to wait until bat file will finish doing its task , please , if you know the answer post me back , thank you

    I have created java script which execute commands
    from cmd levelI'd be surprised if you did. You probably wrote a Java program. I never heard of Javascript being run from the console.
    - In java script I'm starting *. bat file which
    retrieve information from system example :
    hostname > C:\test and then I'd like to
    load from the file C:\test this host
    name example : MyComputer???
    but the problem is that my script execute reading
    from the file C:\test before any information will be
    delivered to this file , how can make a system to
    wait until bat file will finish doing its task ,
    please , if you know the answer post me back , thank
    youWhat are you talking about? This isn't Java, this isn't Javascript, I have no idea what it is. It's already difficult to understand your problem description.

  • Question about Java programming development

    There are five phases in java program development. They are: edit -> compile -> load -> verify -> execute
    However, every time I develops program. I only experience phase 1, phase 2 and phase 5. I always miss phase 3 and 4. When do the class loader and bytecode verifier work? How do they work?
    Second question, how the legitimate programs can execute automatically without typing command java to invoke the interpreter?

    lighthero wrote:
    I still have some questions.
    1. I want to run the program just like the commercial software such as Adobe and MS Office.(just double click to run the program.) How do I execute a java program without typing command java every time?Create an executable JAR: [http://java.sun.com/docs/books/tutorial/deployment/jar/index.html]
    >
    2. How does the JIT compiler work?[http://www.google.ca/search?q=Java+JIT]

  • Change keyboard language by java program

    I have done internationalization successfully in my java web application.
    but for insert input i always need to select language of keyboard manualy by
    control panel's .
    have any one java program to select keyboard language programatically.

    to_anilkumar82 wrote:
    Actualy when you select application in perticuler language (suppose in japanies) than
    message dispalying and value insertion will be done in that language(japanies).
    this is the case when user is not aware about language,user only select a lnguage type
    initialy when user open the application.So what? This is a feature of youyr application, whereas the keyboard layout is not.

  • How to compile and run java program at command console

    hi there
    can anyone tell me how to compile and run a java program at command console? I have installed JRE 1.3.1, and also have installed JBuilder 5 if it helps.

    try this
    System.out.println("Enter your Name : ");
    BufferedReader console = new BufferedReader( new InputStreamReader( System.in ) );
    String s = console.readLine();
    System.out.println("Hello : "+ s+" !" );

  • Open a console program via a java program

    Hello
    I don't know if this is the right forum's section for this question, but:
    I want to open a program inside my java program and I want to receive messages from this running program.
    My java program have to open a program and receive everything what this opened program prints on the screen.
    Can anyone help me?
    Everything will help: function names, examples, etc.
    Thanks!

    This should be the best resource:
    http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html

  • Java programs don't accept input from German Microsoft keyboard on Mac OS X

    Hi everyone,
    I use Microsoft's Natural Ergonomic Keyboard 4000 with a German layout and the current IntelliType Pro driver on a Mac (OS X 10.5).
    When activating the NEK 4000-specific German keyboard layout supplied with IntelliType Pro for Mac, Java programs cannot recognize keyboard inputs anymore. They can again as soon as I switch back to the standard German keyboard layout provided by Apple with OS X 10.5. However, this layout does not match the characters printed on the NEK 4000.
    This phenomon is limited to Java programs running on OS X with the IntelliType Pro's specific German keyboard layout activated. It does not occur with native OS X programs. Therefore, the problem could result from Microsoft's IntelliType Pro driver for OS X, Sun's Java Virtual Machine (in OS X), or Apple Mac OS X.
    Could a Sun employee please take a look at this issue?
    I am more than willing to provide the respective log files if this was needed.

    arne2 wrote:
    Sun's Java Virtual Machine (in OS X), Apple, not Sun, is the source of the VM on that OS.
    Apple probably gets it from Sun but still Apple is responsible for it.

Maybe you are looking for