detecting any keystroke in java - 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

Related

LWJGL - Problem with capturing keyboard events

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.

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

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 if a key (CTRL) is currently pressed without a KeyEvent in Java

I need to be able to see if the CTRL key is currently being pressed during the processing of a mouse event. I have tried to use the KeyListener, but trying to use the mouse event and key event together is proving to be an issue because of focus issues.
What I am essentially trying to accomplish is selecting multiple objects using the CTRL key like in Windows.
It would be much easier if, while in my mouse event, I could just check the status of the CTRL key...
Can you do that in Java?
Thanks.
Use getModifiers() to detect the key pressed..
eg:
if ((event.getModifiers() & ActionEvent.CTRL_MASK) ==ActionEvent.CTRL_MASK) {
System.out.println("CTRL KEY PRESSED");
}
MouseEvent extends from InputEvent, and I think that you can still get the modifiers from this object via getModifiers() to see if a ctrl key has been pressed. I've not tested this yet though.

Categories

Resources