Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
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 07-03-2007, 08:54 PM
Senior Member
 
Join Date: Jun 2007
Posts: 111
Eric is on a distinguished road
Help with pong game
Hi , I am trying to build a pong game. I just need to find some type of Keylistener that will help me know what the person typed. Like I know what they typed if I add a textbox, but I want them to type anywhere in my window and for my program to still be able to know what they did.

Eric
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-03-2007, 08:57 PM
Member
 
Join Date: Jun 2007
Posts: 92
Marcus is on a distinguished road
There are several ways to approach this.

Implement KeyListener:
Code:
import java.applet.Applet; import java.awt.event.KeyListener; import java.awt.event.KeyEvent; public class KeyListenerExample extends Applet implements KeyListener { public void init() { addKeyListener(this); } public void keyPressed(KeyEvent e) { System.out.println("keyPressed: " + e.getKeyChar()); } public void keyTyped(KeyEvent e) { System.out.println("keyTyped: " + e.getKeyChar()); } public void keyReleased(KeyEvent e) { System.out.println("keyReleased: " + e.getKeyChar()); } }
Create an inner private class that extends a class that implements KeyListener, or that implements KeyListener itself:
Code:
import java.applet.Applet; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; public class KeyListenerExample extends Applet { public void init() { addKeyListener(new MyKeyListener()); } private class MyKeyListener extends KeyAdapter { public void keyPressed(KeyEvent e) { System.out.println("keyPressed: " + e.getKeyChar()); } public void keyTyped(KeyEvent e) { System.out.println("keyTyped: " + e.getKeyChar()); } public void keyReleased(KeyEvent e) { System.out.println("keyReleased: " + e.getKeyChar()); } } }
Create an anonymous inner class:
Code:
import java.applet.Applet; import java.awt.event.KeyListener; import java.awt.event.KeyEvent; public class KeyListenerExample extends Applet { public void init() { addKeyListener(new KeyListener() { public void keyPressed(KeyEvent e) { System.out.println("keyPressed: " + e.getKeyChar()); } public void keyTyped(KeyEvent e) { System.out.println("keyTyped: " + e.getKeyChar()); } public void keyReleased(KeyEvent e) { System.out.println("keyReleased: " + e.getKeyChar()); } }); } }
Greetings.

Marcus
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-03-2007, 09:02 PM
Member
 
Join Date: Jun 2007
Posts: 95
Felissa is on a distinguished road
If you look at the JFrame API, you'll notice that it inherits from Component, which provides the addKeyListener(KeyListener) method.
JFrame
Code:
import java.awt.Container; import java.awt.Dimension; import java.awt.event.KeyListener; import java.awt.event.KeyEvent; import javax.swing.JFrame; import javax.swing.JPanel; public class KeyListenerExample extends JFrame { public KeyListenerExample() { super ("KeyListener Example"); JPanel panel = (JPanel) getContentPane(); panel.setPreferredSize(new Dimension(800, 600)); panel.setLayout(null); pack(); setResizable(false); setVisible(true); addKeyListener(new KeyListener() { public void keyPressed(KeyEvent e) { System.out.println("keyPressed: " + e.getKeyChar()); } public void keyTyped(KeyEvent e) { System.out.println("keyTyped: " + e.getKeyChar()); } public void keyReleased(KeyEvent e) { System.out.println("keyReleased: " + e.getKeyChar()); } }); } static public void main(String[] args) { new KeyListenerExample(); } }
Greetings.

Felissa
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
Ping pong game adam405 New To Java 7 03-24-2008 06:29 AM
TicTacToe Game Ebtihal New To Java 0 01-09-2008 01:01 PM
Programming a Game? gt123 New To Java 4 01-01-2008 02:41 PM
Implementing "Game Over" in Minesweeper game based on Gridworld framework. JFlash New To Java 0 11-16-2007 01:02 AM
Need help with random game! silverq_82 New To Java 4 08-07-2007 04:58 PM


All times are GMT +3. The time now is 11:21 AM.


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