• Advertisement
Sign in to follow this  

Java - KeyListener Problem

This topic is 3407 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey GD community. I've been learning programming for a couple weeks now and I have a problem with keylistener. When I call getKeyChar() it prints out the letter I typed but when I call getKeyCode() it always returns 0. Can anyone find the problem? Also I've seen different bracing styles. Is there one preferred over the other? Any criticism on my coding style is appreciated. I want to start out right.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Core extends JComponent implements KeyListener 
{
	private Player player = new Player(100, 600);
	private Shot shot;
	private boolean gameOver = false;
	private int level = 1;
	private Image img;
	
	public Core()
	{
		setFocusable(true);
		addKeyListener(this);
	}
	
	public void nextLevel()
	{
		level++;
	}
	
	public void gameOver()
	{
		gameOver = true;
	}
	
	public void paintComponent(Graphics g)
	{
		Graphics2D g2 = (Graphics2D) g;
		g2.setColor(Color.BLACK);
		g2.fillRect(0, 0, 800, 700);
		img = player.getImage(player.getLevel());
		player.update();
		g2.drawImage(img, player.getX()-14, player.getY(), this);
		for(int i=0; i<player.getNumShots(); i++)
		{
			shot = player.getShots(i);
			if(shot != null)
			{
				img = shot.getImage(shot.getDamage());
				g2.drawImage(img, shot.getX(), shot.getY(), this);
				shot.updatePos();
			}
		}
		try{ Thread.sleep(20); }
		catch(InterruptedException ie){}
		
		repaint();
	}
	
	public void keyPressed(KeyEvent e)
	{
	}
	public void keyReleased(KeyEvent e)
	{
	}
	public void keyTyped(KeyEvent e)
	{
		System.out.println(e.getKeyCode());
		System.out.println(e.getKeyChar());
	}
	
}

Share this post


Link to post
Share on other sites
Advertisement
From the java documentation

Quote:

getKeyCode

public int getKeyCode()

Returns the integer keyCode associated with the key in this event.

Returns:
the integer code for an actual key on the keyboard. (For KEY_TYPED events, the keyCode is VK_UNDEFINED.)

source


so it seems that when you use the KEY_TYPED event it returns VK_UNDEFINED which is 0.

Share this post


Link to post
Share on other sites
I think I remember reading somewhere about this that the reason they made the two are because one is for input from the user and one is for input from the programmer, though I may be wrong about it as it was a long time ago that I even looked at Java.

Share this post


Link to post
Share on other sites
Quote:
Original post by id0001
From the java documentation

Quote:

getKeyCode

public int getKeyCode()

Returns the integer keyCode associated with the key in this event.

Returns:
the integer code for an actual key on the keyboard. (For KEY_TYPED events, the keyCode is VK_UNDEFINED.)

source


so it seems that when you use the KEY_TYPED event it returns VK_UNDEFINED which is 0.


Thanks a lot. This solves it.
I'll read more next time.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement