Article View: borland.public.jbuilder.java.api
Article #124KeyListener kinda deaf (for simple game)
From: nammtar667
Date: Wed, 21 Apr 2004 23:14
Date: Wed, 21 Apr 2004 23:14
93 lines
3263 bytes
3263 bytes
Hey everyone...
I'm trying to write a simple word-guessing game (Lingo, user should
guess 5 letter words etc).
I've created some custom AWT Components (sorry for the dutch naming):
Lettervak: extends Canvas
Woordveld: extends Container and contains Lettervak's
Speelveld: extends Container and contains Woordveld's
The Speelveld (playing field in english) is the main component I want
to talk to from the applet itself.
I don't care where a key is typed, I just want to be able to send the
message to the correct component so the letter can be drawn in a
Lettervak. So I though I'd add a KeyListener to the Applet itself.
It registers (I can see in the debugger), but it doesn't receive any
key events (I can tell by adding a breakpoint there, so it's not in
code further down).
Can anyone tell me what I'm doing wrong? Below is the code of the
Applet itself:
(may look a bit messy, because I've been fooling around to get it to
work, sorry...)
---
package lingo;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class LingoWeb
extends Applet {
private boolean isStandalone = false;
// Components
Speelveld grid;
//Get a parameter value
public String getParameter(String key, String def) {
return isStandalone ? System.getProperty(key, def) :
(getParameter(key) != null ? getParameter(key) : def);
}
//Construct the applet
public LingoWeb() {
this.addKeyListener( new SpeelveldListener() );
}
//Initialize the applet
public void init() {
try {
jbInit();
}
catch (Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
Speelveld grid = new Speelveld( 10, 20, 5, 5, 40 );
this.setFocusable( true );
this.requestFocusInWindow();
this.setLayout(null);
this.add(grid);
this.repaint();
}
//Get Applet information
public String getAppletInfo() {
return "Applet Information";
}
//Get parameter info
public String[][] getParameterInfo() {
return null;
}
class SpeelveldListener implements KeyListener {
public void keyTyped( KeyEvent e ) {
char c = e.getKeyChar();
grid.invoer(c);
}
public void keyPressed( KeyEvent e ) {
System.out.println( e.getKeyChar() );
}
public void keyReleased( KeyEvent e ) {}
}
}
---
Thanks!
Tim.
Technical Details
Message-ID:
<rvvd80t2e84verqh4hr9v64ti2l1mrjbnc@4ax.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!news.glorb.com!newsfeed.kabelfoon.nl!213.51.141.5.MISMATCH!newshub2.home.nl!newshub1.home.nl!home.nl!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 1089d0,5caaf7760005416c,start X-Google-Attributes: gid1089d0,public X-Google-ArrivalTime: 2004-04-21 16:15:02 PST Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!news.glorb.com!newsfeed.kabelfoon.nl!213.51.141.5.MISMATCH!newshub2.home.nl!newshub1.home.nl!home.nl!not-for-mail From: nammtar667@hotmail.com Newsgroups: borland.public.jbuilder.java.api Subject: KeyListener kinda deaf (for simple game) Date: Thu, 22 Apr 2004 01:14:46 +0200 Organization: @Home Benelux Lines: 93 Message-ID: <rvvd80t2e84verqh4hr9v64ti2l1mrjbnc@4ax.com> NNTP-Posting-Host: cp22893-a.dbsch1.nb.home.nl Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Trace: news4.tilbu1.nb.home.nl 1082589284 8588 217.121.1.47 (21 Apr 2004 23:14:44 GMT) X-Complaints-To: abuse@home.nl NNTP-Posting-Date: Wed, 21 Apr 2004 23:14:44 +0000 (UTC) X-Newsreader: Forte Agent 2.0/32.640