Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Key listener or what?  RSS feed

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
Susilo
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think he wants to use keylistener for it... so then for your class name you have
Classname
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 ]
 
Ola gharably
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ola
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!