LWJGL - Problem with capturing keyboard events - java

i have a one problem with LWJGL Keyboard module. I need to detect pressed keys, and i know how to do it. But, when i press key, LWJGL returns me 2-3 events. So for example, if i press 'A', LWJGL calls 3 events, and i getting 'AAA'. How do i fix it? Here is my code:
Keyboard.enableRepeatEvents(true);
if(Keyboard.getEventKeyState()){
if(Keyboard.getEventKey() == Keyboard.KEY_BACK){
set.setValText(""); // Clean text
}else{
set.setValText(set.getFieldText() + Keyboard.getEventCharacter()); // Add event symbol to my string
}
}
Keyboard.next();

I want to make a comment but not enough reputation to do it so I post as an answer
Basically when a key pressed, there is more than 1 event fired, for example: key down, key up, key pressed, key released,...
Arcording to document from LWJGM (I'm not familiar with it, just googled for a while), there is at least 2 events when pressing a key:
#define GLFW_RELEASE 0
The key or mouse button was released.
#define GLFW_PRESS 1
The key or mouse button was pressed.
#define GLFW_REPEAT 2
The key was held down until it repeated.

Related

Android Studio? How long a key is pressed?

I was trying to develop a basic application, I wanted to add feature of how long a key is pressed.
Basically I am trying to make a small game.
I just want to know that how can I detect in application that how long the key is been pressed. However the key must not be pressed multiple times the input must not be given.
The key pressed once, the application should start calculating time from the beginning when the key got pressed to the point when it got released.
Any idea related to this and if code can be given it would be helpful.
Thank you:)
Use a key event listener for when the button is pressed and released, and for both use the System.currentTimeMillis() method. Subtract the first "time" with the later one, and you have how long the button has been pressed

Keycode event for BACKSPACE in JavaFx

I am doing a project in JavaFX, using Java 8 and Gluon scenebuilder. I want to detect when backspace is pressed inside a TextField. This is the code I am using:
public void keyPressed(KeyEvent kEvent) {
if (kEvent.getCode() == KeyCode.BACK_SPACE) {
System.out.println("Backspace pressed");
}
This code is inside the controller file, called FXMLDocumentController, which controls the GUI xml file FXMLDocument. You can see from the image below the function is called whenever a key is typed inside TextField. This works with all the letters/numbers, but not with backspace.
Gluon scene builder settings
Theoretically it should work, but it doesn't.
How can I manage typing of the backspace button?
Edit:
Notice that putting this exact function on the root of the elements, on the "Window itself" (which is the AnchorPane) works. The problem is with reading the pressing of the backspace inside the TextField. You can see in the image below where I've put the function:
On the window backspace's detecting works
You should use your keyPressed method in either on key pressed or on key released.
In the Docs, it states that:
No key typed events are generated for keys that don't generate Unicode
characters (e.g., action keys, modifier keys, etc.).
Backspace is consider an Action Key.
In the screenshot of your SceneBuilder I can see you are referencing the keyPressed(KeyEvent kEvent) controller method with On Key Typed not On Key Pressed.
For events of KeyEvent.KEY_TYPED the value of getCode() is always KeyCode.UNDEFINED.
Edit: I came back to this answer and reread your question. I want to clarify something but what I said above is still correct.
In your edit you mention the "exact" same setup works with the AnchorPane but when looking at your screenshot there are some differences. For your AnchorPane you have the controller method referenced for all three types: On Key Pressed, On Key Released, and On Key Typed.
This means that the method should be called up to 3 times for each key stroke (when the events reach the AnchorPane). Once when pressed, once for typed (if the key is capable of sending typed events - see the answer by Sedrick for clarification), and then once for released. Because of this, the method will work for the pressed and released events, but it won't work for the typed events.
In other words, the reason your code works for the AnchorPane but not the TextField is because you configured it correctly for the AnchorPane but not the TextField.

How can I call function after 5 seconds of last key pressed in java. Any Suggestions?

How can I call function after 5 seconds of last key pressed in java. Any Suggestions ?
How can I call function after 5 seconds of last key pressed in java
This statement implies that the user might be pressing multiple keys and you only want to do the function 5 seconds after the user has stopped pressing keys.
So the solution would be to use a KeyListener on your component to listen for KeyEvents. Then whenever a key is press you restart a Swing Timer. Once the user stops pressing keys the Timer will generate and event and you can invoke your function.
For an example of this approach check out Application Inactivity. It uses this concept to listen for activity for the entire application (key events and mouse events).
Without more detail regarding your specify requirement we can only provide a general approach.

Java get key repetition with keyListener

I have a small game which uses arrow keys. However if I press the arrow keys within a timeout window of say 200ms, the timeout starting from the first key press, how do I capture all the keys pressed using keyListener?

detecting any keystroke in java

I'm writing a program that needs to wait until any key is pressed. For example:
System.out.println("press any key to continue");
//wait until any keystroke
obj.doSomething;
the key that is pressed doesn't matter and I don't need to capture what key was pressed, I just need the program to wait until a key is depressed to continue.
The official Java Tutorials describe how to implement Key Listeners for this purpose:
http://docs.oracle.com/javase/tutorial/uiswing/events/keylistener.html

Categories

Resources