0

Possible Duplicate:
How to listen to keyboard events in a console application

Hi I am new to java syntax.I am trying to invoke an action on a keystroke like "ENTER key"

For ex:

on pressing Enter key {
    int c = 9+8 ;    //This should run on pressing enter key but not just running program directly
    printf("c");
}

I have referred few java doc relating to this and tried to execute the code.But I face few syntax errors. Can any one give direct example

Community
  • 1
  • 1
user1677685
  • 25
  • 1
  • 7
  • Are you using java.awt? Can you specify technology name? – RAS Sep 17 '12 at 13:24
  • No. im running a console type program – user1677685 Sep 17 '12 at 13:33
  • Yes, but to capture "Enter" key press, you must have something like TextBox, TextArea etc. Which TextBox are you using? java.awt.TextField? – RAS Sep 17 '12 at 13:38
  • 1
    @user1677685 Have a look at this question: [How to listen to keyboard events in a console application](http://stackoverflow.com/questions/9075769/how-to-listen-to-keyboard-events-in-a-console-application) – Baz Sep 17 '12 at 13:40
  • @RAS Nope its a console program where it should read enter key and run the program and give output. Im new to java programming just wanted to try this way – user1677685 Sep 17 '12 at 13:42

1 Answers1

0

On a command line application, I believe you'd have to parse input from System.in. If you are only wanting to supply initial parameters, there are libraries out there that can do this for you to make your life easier such as Commons CLI. If you are running from the command line, you will be compiling your code to a .jar file. This library should let you supply flags, and named arguments to your java classes within your jar file.

To output text to the command prompt, use System.out.

aglassman
  • 2,643
  • 1
  • 17
  • 30