Page 1 of 1

Video Game Applet pt 2 Adding Async Keyboard input Rate Topic: ****- 2 Votes

#1 WolfCoder  Icon User is offline

  • Isn't a volcano just an angry hill?
  • member icon


Reputation: 782
  • View blog
  • Posts: 7,610
  • Joined: 05-May 05

Post icon  Posted 16 January 2006 - 04:35 PM

I was going to tech about MouseListeners for games but Asyncronized keyboard input is better for action games.

This is a rather short tutorial but it's valuable. Consider the following class definition:
import java.awt.event.*;
public class GameKey implements KeyListener // Keyboard input
{
	public boolean key_right,key_left,key_up,key_down,key_z,key_x;
	public void keyTyped(KeyEvent e){}
	public void keyPressed(KeyEvent e)
	{
  if(e.getKeyCode() == e.VK_DOWN)
 	 key_down = true;
  if(e.getKeyCode() == e.VK_UP)
 	 key_up = true;
  if(e.getKeyCode() == e.VK_LEFT)
 	 key_left = true;
  if(e.getKeyCode() == e.VK_RIGHT)
 	 key_right = true;
  if(e.getKeyCode() == e.VK_Z)
 	 key_z = true;
  if(e.getKeyCode() == e.VK_X)
 	 key_x = true;
	}
	public void keyReleased(KeyEvent e)
	{
  if(e.getKeyCode() == e.VK_DOWN)
 	 key_down = false;
  if(e.getKeyCode() == e.VK_UP)
 	 key_up = false;
  if(e.getKeyCode() == e.VK_LEFT)
 	 key_left = false;
  if(e.getKeyCode() == e.VK_RIGHT)
 	 key_right = false;
  if(e.getKeyCode() == e.VK_Z)
 	 key_z = false;
  if(e.getKeyCode() == e.VK_X)
 	 key_x = false;
	}
}



It is a simple class that implements a KeyListener interface. It contains some booleans on the current state of the key. All of that key repeat rate stuff is bad for video games. Since the events are fired to key repeat rate specifications (There is a short pause until the event is fired continuously), this type of code is needed.

The boolean is set to true when THE VERY FIRST TIME the keyPressed event is fired.

To maintain the asyncronized input, the boolean is set to false immeadiatly when the key is released.

Now, here is a code cutout of how you would implement this into a panel or what have you:

setFocusable(true); // Required for keyboard input
GameKey game_key = new GameKey(); // Variable needed to get key states
addKeyListener(game_key); // Add it to your game



And when you want to read from the current state of the keyboard input, you simply do this:

if(game_key.key_z)
{
   // Key was pressed
}



If you want your own keys defined, add the corresponding code to keyPressed and keyReleased.

There you have it.

Is This A Good Question/Topic? 2
  • +

Replies To: Video Game Applet pt 2

#2 Fame Commeth  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 13-August 05

Posted 05 March 2006 - 01:18 PM

Wow! That solves so many problems! Thanks!

hehe:

postcount++;
Was This Post Helpful? 0
  • +
  • -

#3 jhoy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 31-January 08

Posted 03 February 2008 - 05:47 AM

View PostFame Commeth, on 5 Mar, 2006 - 01:18 PM, said:

Wow! That solves so many problems! Thanks!

hehe:

postcount++;

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1