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
Related
I want to reassign event, when user send KEYEVENT_SEARCH. Now it just start search activities in applications and if you press it second time - it doesn't do anything. I have to redefine the event so that the first click opens the search, and the next time it closes. Where can I find android push responses? I try edit it in frameworks/base/core/java/com/android/internal/policy/PhoneFallbackEventHandler.java , but it doesn't works.
Android keys are processed in the following sequence:
PhoneWindowManager:
interceptKeyBeforeQueueing
interceptKeyBeforeDispatching
Activity: onKeyDown/onKeyUp. App can override the function to handle keys.
PhoneFallbackEventHandler: onKeyDown/onKeyUp
Currently, the search key is handled in 2. When handling search key in onKeyUp, firstly check whether the search dialog is launched, then choose to startSearch or closeSearch.
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.
I am a beginner in programming android.
Is there any way to combine two long press buttons with an Android service and react to that?
I want to combine two long press buttons (home & volume down ) for activate wifi even if the phone is locked.
thanks.
Rather than connecting to the long press listener..
Connect to the button press listener.
Check when the button is pressed down, and when it is lifted up.
Measure the time between these 2 events. Was it long enough to be classed as a long press?
Were both buttons pressed down long enough at the same time?..
If you want to trigger while they are down still, just ensure the up event hasnt fired yet after 2 seconds for example.
Is it possible to have my android application start a method from pressing of the convenience key when the application in focus?
On my phone the convenience key is a button located on the right hand side of the phone. I would like to be able to know if the user presses any of the buttons on the phone, even the volume would work.
You are welcome to override onKeyDown() on your Activity and watch for a KeyEvent of interest. Note that not every key in the KeyEvent JavaDocs is accessible this way (e.g., power). Also note that it is possible that some specific View that is aware of these keys might consume the event first, though that is relatively unlikely for anything that matches your description of a "convenience key".
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.