Keeping a key pressed - java

In my application I would like to add an key event where a couple of keys are constantly pressed.
If I go off my application, they're still pressed. Its kind of hard to explain but I would like my computer to think I am pressing these keys or mouse down.

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

LibGDX: Cannot press spacebar, up and left at same time

I was wondering if there was some sort of workaround for this. Basically the problem is that, when using LibGDX InputProcessor's keyDown/Up, it never allows me to have spacebar, up arrow key and left arrow key pressed at same time. I read online that it is a somewhat common problem with arrow keys that isn't exclusive to libgdx.
I was wondering if there is a workaround (besides switching to WASD). Thanks.
Edit, I wanted to add some details to prove it's not an issue with my code:
I ran some tests to make sure before posting here. The issue is that when, for example, spacebar and left arrow are pressed, keyDown won't execute if the player presses up arrow. Basically whenever there is at least 2 of these pressed, the other one won't update. For some reason however, it does work with right arrow instead of left.
Edit 2: Using Gdx.input.isKeyPressed(int key) doesn't work either.

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?

regain focus after java.awt.Robot.keyPress()/mousePress()

I writing an application which controls another application by using the keyboard only. To more concrete, the application simulates key presses and mouse clicks when a certain key is pressed on the keyboard. For example, pressing on the 'x' key simulates a mouse click on the [X] in the rop right corner, followed by a little sleep of 2 seconds and an 'enter' to confirm the exit dialog. Pretty easy. I am developing this application in Java.
Sending a key press or a mouse click is very easy with java.awt.Robot. I am facing one little problem. Say I have configured a key which will click somewhere on the screen. The problem is that consecutive key presses aren't catched anymore, as my application lost its focus caused by the mouse click outside it's window.
My question now is: what is the best way to be sure that my main application keeps the focus? Is there a way to focus my application again after the key presses and mouse clicks are sent out? Is there a better way?
Thanks in advance.
If your application lost the focus. because you or your Robot clicked to somwhere else, the Robot must click on the application again before sending a new key. In c/c++ you could force the focus to the application (a non-trivial task), not in Java!
You might want to take a look at Component.requestFocus() to see if can do what you want.
Be aware however that window focusing has very platform dependent behaviour, so you will probably need to do quite a bit of testing to ensure that your code does what you want in all circumstances.
I managed a way to prevent applications from losing all focus in Java.
By placing a WindowFocusListener on the frame (or dialog) and calling setVisible(false) followed by setVisible(true) in windowLostFocus the component will re-appear as soon as it is dissapears (not the prettiest solution but it does work).
By then calling component.requestFocus() your robot should be able to continue where it left off

How are multi-button presses handled in swing?

I'm experiencing some strange behaviour when using a stylus with swing.
I am interpreting pressing the button on the side of the stylus(RIGHT) and pressing the stylus down(LEFT) as a "Grab" event, but occasionally (more often than 0), events are just being dropped.
The JavaDocs for MouseEvent are pretty explicit about how multibutton presses are handled if executed one at a time (left down, right down, right up, left up) but say nothing about simultaneous button presses.
I'm left to wonder, would they be emitted as two mousePressed events, or as one with the button mask set for both buttons, or something else entirely?
Thanks.
I'd interpret the API doc as simultaneous button presses being simply not possible:
When multiple mouse buttons are pressed, each press, release, and click results in a separate event.
So there should be separate events. The problems you observe could be due to errors in your code, the stylus' driver, the hardware, or Swing (this is in decreasing order of likelihood as I see it :)
I'd try to diagnose the problem by logging events at different levels, if possible.
Simultaneous button presses are processed as two separate mousePressed events. Run the Mouse Events Demo to see them processed separately.
As I recall, there's no way to handle simultaneous button presses. What I used to do to ensure that multiple buttons being pressed at once were treated as such was I would have a boolean variable for each button and when it was pressed, I would set it to true and when it was released, I would set the boolean to false. Then when it came time to perform an action, I would check for the boolean variables (sometimes I would have the actionlistener redirect to the method call for determining what action was to happen next after setting the booleans). This doesn't work if the only thing you want to do is them being pressed at the exact same time, but if you're just trying to get combinations to work, then that's how I did it. This was about 4 years ago, before Java 5, so I may be wrong about that.

Categories

Resources