Skip to main content
GameDev.net gamedev.net
🔒 Locked

[java] Keyboard question

Started by Stompy9999 Sep 18, 2005 at 4:37 PM 1 replies 1.2k views
Original Post
Stompy9999
Stompy9999
Got another noob Java question for you guys that I couldn't seem to find on google. I'm currently reading this tutorial on keyboard input. Well, I realize that if I implement an Interface, in this case KeyListener, I have to also implement all of its methods, which I do. I've implemented them as such:

public void keyPressed(KeyEvent e)
{
	switch(e.getKeyCode())
	{
		case KeyEvent.VK_RIGHT:
		   player.moveRight();
		   break;
		case KeyEvent.VK_LEFT:
		   player.moveLeft();
		   break;
	}
}
	
public void keyReleased() {}
public void keyTyped() {}


when I compile it, I get this error:
Quote:
Atlas is not abstract and does not override abstract method keyReleased(java.awt.event.KeyEvent) in java.awt.event.KeyListener
I'm not exactly sure what this means cause I get this error whether I override keyRelease or not. EDIT: By the way, Atlas is the name of my main class
H_o_p_s
H_o_p_s
I think if you Change

public void keyReleased() {}
public void keyTyped() {}

to

public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}

that it should work...

-edit-
You could also do something like:

import	java.awt.event.KeyListener;import	java.awt.event.KeyEvent;public class Keyboard implements Keylistener {	boolean[] kp; //Keys pressed	public Keyboard() {		kp = new boolean[256];		for(int i = 0; i < 256; i++) {			kp = false;		}		//MainWindow.addKeyListener(this);//Add this to the window	}	public boolean isPressed(int keyCode) {		return kp[keyCode];	}	public void keyPressed(KeyEvent e)  {		kp[e.getKeyCode()] = true;	}	public void keyReleased(KeyEvent e) {		kp[e.getKeyCode()] = false;	}	public void keyTyped(KeyEvent e)    {}}
And then have an update() that goes something like:

public void update() {
if (Keyboard.isPressed(KeyEvent.VK_DOWN))
//DO STUFF
}

Hope this helps...
BRING BACK THE BLACK (or at least something darker)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.