Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-14-2008, 07:25 PM
Member
 
Join Date: Feb 2008
Posts: 1
oomanamemni is on a distinguished road
how to read single Keystroke without using return
I'm wanting to be able to read in a single keystroke from the user to proceed to the next task but can't seem to find out how to without having to press enter afterwards. Is there any way to do this? Thanks
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 02-14-2008, 08:21 PM
Member
 
Join Date: Jan 2008
Location: South Africa
Posts: 18
jimm1 has a little shameless behaviour in the past
Is this via the console? If you want to register keystrokes, you will need to create a JFrame (Swing GUI), and then add a KeyListener, and then when the user presses a key, it will fire the "keyPressed" method, and from there you can catch the key pressed from the KeyEvent object which has been passed. You will need to read some JFrame/Swing tutorials, which explain Components, Listeners (Such as MouseListener and KeyListener) and you will also need to understand interfaces/inheritance (You implement or extend the KeyListener or KeyAdaptor).

Like:

JFrame frame = new JFrame();
frame.addKeyListener(this);

Then in the same class, you implement "KeyListener" and implement the abstract methods keyPressed(KeyEvent e), keyReleased(KeyEvent e), and keyTyped(KeyEvent e).

Then to get the keystroke, you call one of the methods of the KeyEvent e, such as:

char keychar = e.getKeyChar();

or

int keycode = e.getKeyCode();

-------------------------------

Hope that helps.

For full help, read this Swing Tutorial trail:
Trail: Creating a GUI with JFC/Swing (The Java™ Tutorials)

It explains all about Swing, and also look for the section on "Writing Event Listeners".
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Compiling a single class ROSOBORONEXPORTCORP New To Java 2 01-28-2008 01:27 PM
convert a lot of images into a single one leonard New To Java 1 08-06-2007 05:47 PM
variable to accept a single object Rgfirefly24 New To Java 1 08-06-2007 05:41 AM
Help with import AWTKeyStroke and swing.KeyStroke susan AWT / Swing 1 07-29-2007 11:53 PM
File Upload - Single to allow the search of .txt files Daniel Advanced Java 1 06-06-2007 05:20 AM


All times are GMT +3. The time now is 01:20 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org