It's not a secret anymore!*
The moose likes Beginning Java and the fly likes Key Adapter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Key Adapter" Watch "Key Adapter" New topic
Author

Key Adapter

Martin vanPutten
Ranch Hand

Joined: Mar 26, 2006
Posts: 124
My applet for compile and I don't understand why. This is my code:


This is my html:



These are my errors:


Live And Let Learn.
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
The import statement does not import members of subpackages.

You have to explicity import those classes.

import java.awt.event.*;
Martin vanPutten
Ranch Hand

Joined: Mar 26, 2006
Posts: 124
Now I get this error:
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
That looks like a problem with your drive.
Martin vanPutten
Ranch Hand

Joined: Mar 26, 2006
Posts: 124
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

Joined: Feb 07, 2005
Posts: 2367
Try using keyTyped instead of keyPressed. I'm not sure if that will fix the problem, but it might.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
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

Joined: Mar 26, 2006
Posts: 124
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

Joined: Jan 24, 2006
Posts: 802
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 ]

You down with OOP? Yeah you know me!
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
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.
 
subject: Key Adapter
 
Similar Threads
Double Buffering Problem
Making Y--
Computer Pad Not Stopping!
Problem with writing code.
slowing down movement in an applet.... doesn't have anything to do with the applet.