Having Problems With The Key Listener

Man I spent hours scratching my head on this one. I have never had a problem with input before but I am kinda changing things up a little. Man I can’t wait to stop letting myself get these irritating errors.

This is where I added it to the canvas.

public Main()
	{
		initGame();
		
		JFrame frame = new JFrame(gameMain.getName());
		frame.setIconImage(gameMain.setLogo());
		Canvas canvas = new Canvas();
		canvas.setPreferredSize(gameMain.getScreenSize());
		
		InputListener input = new InputListener();
		canvas.addKeyListener(input);
		canvas.addMouseListener(input);
		
		frame.add(canvas);
		frame.pack();
		frame.setVisible(true);
		canvas.setVisible(true);
		canvas.requestFocus();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		canvas.createBufferStrategy(2);
		buffer = canvas.getBufferStrategy();
	}

This is the simple code to move in my player class and I did import the inputlistener.

	
public void input()
	{
		InputListener input = new InputListener();

		if(input.getKey() == "w")
			dir(0,-1);	
		if(input.getKey() == "a")
			dir(0,1);
		if(input.getKey() == "s")
			dir(-1,0);
		if(input.getKey() == "d")
			dir(1,0);
	}

Here is the InputListener class.

package com.base.engine;

import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

public class InputListener implements KeyListener, MouseListener, MouseMotionListener
{
	Dimension mousePos;
	boolean clicked = false;
	boolean keyDown;
	boolean keyUp;
	String key;
	String tempKey;
	

	public Dimension mouseLoc()
	{
		return mousePos;
	}

	public void mouseMoved(MouseEvent e) 
	{
		mousePos = new Dimension(e.getX(), e.getY());
	}

	public void mousePressed(MouseEvent e) 
	{
		clicked = true;
	}

	public void mouseReleased(MouseEvent e) 
	{
		clicked = false;
	}

	public void keyPressed(KeyEvent e) 
	{
		keys(e.getKeyCode());
		keyDown = true;
	}

	public void keyReleased(KeyEvent e) 
	{
		keyDown = false;
	}

	public void keyTyped(KeyEvent e) 
	{

	}
	
	public void keys(int i)
	{
		if(i == KeyEvent.VK_A)
			key = "a";
		if(i == KeyEvent.VK_B)
			key = "b";
		if(i == KeyEvent.VK_C)
			key = "c";
		if(i == KeyEvent.VK_D)
			key = "d";
		if(i == KeyEvent.VK_E)
			key = "e";
		if(i == KeyEvent.VK_F)
			key = "f";
		if(i == KeyEvent.VK_G)
			key = "g";
		if(i == KeyEvent.VK_H)
			key = "h";
		if(i == KeyEvent.VK_I)
			key = "i";
		if(i == KeyEvent.VK_J)
			key = "j";
		if(i == KeyEvent.VK_K)
			key = "k";
		if(i == KeyEvent.VK_L)
			key = "l";
		if(i == KeyEvent.VK_M)
			key = "m";
		if(i == KeyEvent.VK_N)
			key = "n";
		if(i == KeyEvent.VK_O)
			key = "o";
		if(i == KeyEvent.VK_P)
			key = "p";
		if(i == KeyEvent.VK_Q)
			key = "q";
		if(i == KeyEvent.VK_R)
			key = "r";
		if(i == KeyEvent.VK_S)
			key = "s";
		if(i == KeyEvent.VK_T)
			key = "t";
		if(i == KeyEvent.VK_U)
			key = "u";
		if(i == KeyEvent.VK_V)
			key = "v";
		if(i == KeyEvent.VK_W)
			key = "w";
		if(i == KeyEvent.VK_X)
			key = "x";
		if(i == KeyEvent.VK_Y)
			key = "y";
		if(i == KeyEvent.VK_Z)
			key = "z";
	}
	
	
	public String getKey()
	{
		
		return key;
	}
	
	public boolean getKeyPress()
	{
		return keyDown;
	}

	@Override
	public void mouseDragged(MouseEvent e) {}
	@Override
	public void mouseClicked(MouseEvent e) {}
	@Override
	public void mouseEntered(MouseEvent e) {}
	@Override
	public void mouseExited(MouseEvent e) {}
}

So…you gonna talk about these errors a bit? Or are we just admiring your code?

I was letting it bake in a little ;). I’m just trying to move the the character and I have done it several different ways. Even had the keylistener in the player class with the code in the keypressed method and it still wasn’t working. I’m just lost I don’t really see much difference between the other games I was doing.

Well, the InputListener that you initialize in the input() method is not the same InputListener that you added to your canvas. So, that’s one issue. Nothing is actually listening to the key presses. You should only have one InputListener that you pass around.

EDIT: Actually, you should just poll the keys in your main class, and pass the results to your player.

Sweet thanks a lot seems like something I should have noticed…