Forums Register Login
Key listener or what?
Hi all,
I'm working on some program that should read any number, of course to some limit i know, and based on it do some operation.
Well I should enter this number from the keyboard without a textField or anything else. i.e. i should just when i press on any number ,for example number 20, and press Enter I want to store that value for later use. How can i do that?
Hello Ola,
hope this example can help :
BufferedReader reader = new BufferedReader (new InputStreamReader (System.in));
System.out.print ("Enter number : ");
String sNumber = reader.readLine();
int iNumber = Integer.parseInt (sNumber);
I think he wants to use keylistener for it... so then for your class name you have
implements KeyListener
and then you must include the methods
public void keyPressed(keyEvent e)
public void keyTyped(KeyEvent e)
public void keyReleased(KeyEvent e)
The methods can all be blank except keyTyped if you just want to read the key, so in keyTyped you could do e.getKeyCode() which would tell you which key was pressed. If the key pressed was a number, then you could store it into an array. And if the key pressed was enter, it could proccess what was stored in the array.
As for how to check if they key pressed was a number, I think you can find the different variable constants at the sun java class listing.
Here's an example of using keyPressed in one of my progs:

Can anyone confirm if I'm right or not? I want to help other people but I think maybe I should stick to being helped .
[ July 07, 2003: Message edited by: Matt Fyffe ]
hi Fyffe,
your answer was really perfect.. u r absolutly right.. well i'm now using it in my program and its working perfect.. good luck Matt

This thread has been viewed 816 times.

All times above are in ranch (not your local) time.
The current ranch time is
Oct 15, 2018 17:12:54.