Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Key Adapter

 
Martin vanPutten
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My applet for compile and I don't understand why. This is my code:


This is my html:



These are my errors:
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The import statement does not import members of subpackages.

You have to explicity import those classes.

import java.awt.event.*;
 
Martin vanPutten
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now I get this error:
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That looks like a problem with your drive.
 
Martin vanPutten
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I fixed the driver problem... and it compiles fine. Why doesn't it move at the press of the up and down keys?
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try using keyTyped instead of keyPressed. I'm not sure if that will fix the problem, but it might.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you are using KeyAdapter(), which means the required methods of the KeListener
interface are pre-written (empty), and you override the method/s you want

public void KeyPressed (KeyEvent keP)
does not override
public void keyPressed (KeyEvent keP)//small k

instead it creates a new method, so that when the KeyListener comes looking
for keyPressed(..), it finds an empty method and does nothing

when you get these fixed, you may also have to click into the applet,
to ensure it has the focus.
 
Martin vanPutten
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not understanding! If you mean that keP has lowercase k, then I changed it to uppercase (and I didn't think it would make a difference)it still doesn't work.
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
your not doing the class thing right again...

public class upndownAdapter extends KeyAdapter
{
public void keyPressed(KeyEvent event)
{}
public void keyReleased(KeyEvent event)
{}
}



and you have to define for all directional keys..

up,down,left,right.

if left and right don't do anything, define them as empty...

but when you do

addKeyListener(new KeyAdapter())

you have to make a new adapter...

like so

addKeyListener (new KeyAdapter(upndownAdapter));


that should fix it...

-Justin-
[ April 26, 2006: Message edited by: Justin Fox ]
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the methods are
keyPressed(..) small k
keyReleased(..) small k, and with a d

and I'll re-add
when you get these fixed, you may also have to click into the applet,
to ensure it has the focus.

if it still doesn't work, change the KeyAdapter to KeyListener, add an empty
keyTyped(), and the compiler will tell you when you have it right

[edit]
and when you do get it right, up goes down, down goes up.
[ April 26, 2006: Message edited by: Michael Dunn ]
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic