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

Java - KeyListener Problem

Started by sazanality Dec 26, 2008 at 5:31 PM 2 replies 2.1k views
Original Post
sazanality
sazanality
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());
	}
	
}
id0001
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.
Durakken
Durakken
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.
sazanality
sazanality
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.

Topic Locked

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

Sign in to reply to this topic.