Hello Guest

How to listen key pressed event?

  • 5 Replies
  • 12455 Views
*

Offline Daslee

  • ***
  • 126
How to listen key pressed event?
« on: February 07, 2013, 22:34:38 »
Hello, I'm trying to make game not using any game engines, but have problem in keyboard input. This is hardest part for me, to make keyboard listen for key press event, not key down. Here is my current Keyboard.java code:

Code: [Select]
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class Keyboard implements KeyListener {
private static boolean keys[] = new boolean[65535];

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

public void keyReleased(KeyEvent e){
keys[e.getKeyCode()] = false;
}

public void keyTyped(KeyEvent e){

}

public static boolean isKeyDown(int keyCode){
return keys[keyCode];
}
}

When I create jframe I do this:
Code: [Select]
//At the top of class
public Keyboard keyboard = new Keyboard();

//On init jframe:
addKeyListener(keyboard);

And how I could listen for key press in other classes? For example how it would be with LWJGL:
Code: [Select]
while(Keyboard.next()){
if(Keyboard.getEventKeyState()){
//blabla...
}
}

But I actually do not know what does next() method and getEventKeyState() method. Maybe someone were working with keyboard input and knows how to create those two methods?

*

Offline quew8

  • *****
  • 569
  • Because Square Eyes Look More Real
Re: How to listen key pressed event?
« Reply #1 on: February 08, 2013, 16:55:20 »
Have you read LWJGL's javadocs? http://www.lwjgl.org/javadoc/ It's all there.

*

Offline Daslee

  • ***
  • 126
Re: How to listen key pressed event?
« Reply #2 on: February 08, 2013, 20:56:53 »
That didn't helped me, but I anyway made it. :)
Thanks for help anyway.

*

Offline Kajos

  • *
  • 24
Re: How to listen key pressed event?
« Reply #3 on: February 24, 2013, 15:05:35 »
Could you tell how you solved it?

*

Offline Daslee

  • ***
  • 126
Re: How to listen key pressed event?
« Reply #4 on: February 24, 2013, 16:29:55 »
Sure
Code: [Select]
import java.awt.event.*;

public class Keyboard implements KeyListener {
private static boolean keys[] = new boolean[65535];
private static int lastCheckPressed = -1;
private static int lastPressed = -1;

public void keyPressed(KeyEvent e){
keys[e.getKeyCode()] = true;
if(lastCheckPressed != e.getKeyCode()){
lastPressed = e.getKeyCode();
lastCheckPressed = e.getKeyCode();
}
}

public void keyReleased(KeyEvent e){
keys[e.getKeyCode()] = false;
lastCheckPressed = -1;
}

public void keyTyped(KeyEvent e){
}

public static int getLastPressed(){
int last = lastPressed;
lastPressed = -1;
return last;
}

public static boolean isKeyDown(int keyCode){
return keys[keyCode];
}
}

Usage:
Code: [Select]
switch(Keyboard.getLastPressed()){
case KeyEvent.VK_ENTER:
System.out.println("Haha");
break;
}

Keyboard class add as key listener in jframe class or whatever, like this:
Code: [Select]
addKeyListener(new Keyboard());

*

Offline kappa

  • *****
  • 1316
Re: How to listen key pressed event?
« Reply #5 on: February 24, 2013, 16:46:28 »