0
Keyboard for game in java
Hey guys. I'm developing a kind of game engine to build a game on top of it. I have to write a keyboard class that I want it to be able to response one time per tap or make response while it is pressed. Can you give me any idea?
6 Respostas
+ 1
On the computer, yes. On the phone, no... I’m sorry!
+ 1
you could try to create a class that implements the KeyListener interface (import java.awt.event.KeyListener).
0
I need it on computer.
I know that I need keylistener. I ask about how to handle just one tap and how to handle the release of button.
0
Actually I don't know what is your goal, but I hope that these codes could help you for your purposes:
--First you can copy and paste this class
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JComponent;
import javax.swing.KeyStroke;
public class KeyComponent extends JComponent{
/**
* This component has to extends a JComponent
* in order to accept a listener
*/
private static final long serialVersionUID = 1L;
//This inner class implements the KeyListener interface
class KeyReceiver implements KeyListener{
//This method has to elaborate what happens after the key click
@Override
public void keyPressed(KeyEvent e) {
/***
* First I want to create a String which gives me information
* about
* which keyboard button I pressed
*/
String key = KeyStroke.getKeyStrokeForEvent(e).toString();
/***
* I want to print on the console the message about the keyboard
* button I pressed;
* you can try to insert if statements that decide which method
* has to be invoked
* (like "if(key.equals("pressed DOWN"){...})
*/
System.out.println(key);
}
/***This method has to elaborate what happens after the key has been
*/ released
@Override
public void keyReleased(KeyEvent e) {
/***The same as the "keyPressed" method; in this case is related
*/to the button release
String key = KeyStroke.getKeyStrokeForEvent(e).toString();
System.out.println(key);
}
@Override
public void keyTyped(KeyEvent e) {
}
}
public KeyComponent()
{
this.addKeyListener(new KeyReceiver());
/***
* the most important thing about KeyListener is that the component
* which
* adds the listener has to invokes these two methods below!
*/
this.setFocusable(true);
this.requestFocus();
}
}
0
--Then you can copy and paste in the same package of the first class the following one and run it; let me know what will happen...
import javax.swing.JFrame;
public class KeyFrame extends JFrame{
/**
* I want to instantiate a JFrame only has a support for the KeyComponent
*/
private static final long serialVersionUID = 1L;
private static final int WIDTH_FRAME = 200;
private static final int LENGHT_FRAME = 200;
private static KeyComponent k;
public KeyFrame()
{
this.setSize(WIDTH_FRAME, LENGHT_FRAME);
//I have to add the KeyComponent to the frame
k = new KeyComponent();
this.add(k);
}
public static void main(String[] args) {
KeyFrame frame = new KeyFrame();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/***Once you have run the main method, try to tap some button and
*/ see what happens
}
}
0
I found an other solution but I will try your way too