I am new to android development, making a simple game and using onKeyDown(.....) function but it works only for 1 key at a time, how to handle 2 keys at a time means can i move right or left with continous firing.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
switch (keyCode){
case KeyEvent.KEYCODE_DPAD_RIGHT:
bottle_movx+=2;
break;
case KeyEvent.KEYCODE_DPAD_LEFT:
bottle_movx-=2;
break;
case KeyEvent.KEYCODE_DPAD_DOWN:
fire=bottle_movx;
firechk=true;
break;
}
invalidate();
return super.onKeyDown(keyCode, event);
}
EDIT: Perhaps this could be of some help?
How do I handle simultaneous key presses in Java?
Quote:
One way would be to keep track yourself of what keys are currently down.
When you get a keyPressed event, add the new key to the list; when you get a keyReleased event, remove the key from the list.
Then in your game loop, you can do actions based on what's in the list of keys.
EDIT 2: I also found this link which could be useful:
How do I handle multiple key presses in a Java Applet?
Please also check the action event( event.getAction() ). This would return the constant defined in the KeyEvent class like ACTION_DOWN, ACTION_UP etc. Please check for either up or down action.
Related
I have created android keyboard and I am creating arrows to move the cursor in the user input, I could do the left and right but I couldn't know how to write the code for the up and down .. here is the code
switch (arrow){
case KEY_LEFT:
CharSequence leftText = getCurrentInputConnection().getTextBeforeCursor(1000, 0);
int leftLen = leftText.length() ;
getCurrentInputConnection().setSelection(leftLen-1, leftLen-1);
break;
case KEY_RIGHT:
CharSequence rightText = getCurrentInputConnection().getTextBeforeCursor(1000, 0);
int rightLen = rightText.length() ;
getCurrentInputConnection().setSelection(rightLen+1, rightLen+1);
break;
case KEY_UP: case KEY_DOWN:
break;
}
can any one help me implementing the down and up ??
If you just want to move cursors
https://developer.android.com/reference/android/inputmethodservice/InputMethodService.html#sendDownUpKeyEvents(int)
Try use sendDownUpKeyEvents method
sendDownUpKeyEvents(KeyEvent.KEYCODE_DPAD_RIGHT);
Will simply do the work.
First of all, I recommend doing something like this to check for key presses
myEditText.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v,
int keyCode,
KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_LEFT:
moveCursorLeft();
return true;
case KeyEvent.KEYCODE_DPAD_RIGHT:
moveCursorRight();
return true;
case KeyEvent.KEYCODE_DPAD_UP:
moveCursorUp();
return true;
case KeyEvent.KEYCODE_DPAD_DOWN:
moveCursorDown();
return true;
}
}
return false;
}
});
I'm going off the assumption that you're programming a text editor of some sort.
The implementation for up and down depends on what you want those buttons to do, and how you've implemented things so far. For example, you could have each line as its own EditText that is kept track of in an ArrayList. Then, each time you add a new line, you can add a new EditText to the ArrayList. And when a user presses up or down, you can just change the index of the ArrayList by 1 in the correct direction.
I imagine that would be a pretty inefficient way of doing it, but it would be easy to define what up and down do. It's difficult to say more about what you should do without seeing more of your code.
I apologise for the somewhat noobish question.
I've tried googling it, searching this site and the android developer site, yet didn't manage to find an answer.
I'm looking for a listener that differentiates between up and down events. Currently I'm using OnTouchListener, but it gets triggered very fast, causing a noticable lag, even when I immediately return after an event that is not Down and Up.
I've also tried OnClickListener, but it seems to only get triggered when you lift your finger up, rather than have seperate events for down click and up click like I need.
Would appreciate some help,
Thanks!
YourBtn.setOnTouchListener(new View.OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event){
if(event.getAction() == MotionEvent.ACTION_UP){
//do stuff
}
else if(event.getAction() == MotionEvent.ACTION_DOWN){
//do stuff
}
else if(event.getAction() == MotionEvent.ACTION_CANCEL){
//do stuff
}
return true; //return false can be used it depends
}
});
P.S: it can be: a button, a textview, an imageview or any UI element
I strongly recommend to always add ACTION_CANCEL to avoid any misbehaviours or crashes
See this:
https://developer.android.com/training/graphics/opengl/touch.html
Override the following method on your activity, then you can treat each case, when the user touch the screen, and when he take of his finger.
#Override
public boolean onTouchEvent(MotionEvent e) {
// MotionEvent reports input details from the touch screen
// and other input controls. In this case, you are only
// interested in events where the touch position changed.
float x = e.getX();
float y = e.getY();
switch (e.getAction()) {
case MotionEvent.ACTION_DOWN:
// do your stuff
break;
case MotionEvent.ACTION_UP:
// do your stuff
break;
}
return true;
}
You should look at GestureDetector to simplify your touch event handling.
I have a custom view that captures the user's signature (John Hancock). I want our application to be as accessible as possible, so I'm taking special care to ensure to optimize our screens for TalkBack and Explore-by-Touch. Since Explore-by-Touch changes all one finger gestures into two finger gestures, it breaks the custom signature view.
What I'd like to do is have Explore-by-Touch announce the view's content description on hover, then enable the view when the user double-taps. This will allow them to draw on top of the view with a single pointer like a normal user.
I've searched around but it's difficult to find detailed documentation on the Android accessibility libraries. Any ideas?
When Explore by Touch is turned on, single-finger touch events are converted into hover events. You can watch these events by adding an OnHoverListener to your view or overriding View.onHoverEvent.
Once you're intercepting these events, you can usually just pass them to your normal touch handling code and map from hover actions to touch actions (as below).
#Override
public boolean onHoverEvent(MotionEvent event) {
if (mAccessibilityManager.isTouchExplorationEnabled()) {
return onTouchEvent(event);
} else {
return super.onHoverEvent(event);
}
}
#Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_HOVER_ENTER:
return handleDown(event);
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_HOVER_MOVE:
return handleMove(event);
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_HOVER_EXIT:
return handleUp(event);
}
return false;
}
I am trying to find out the Action Name that is being performed by the mouse. I tried
String act=String.valueof(event.getAction());
it returns me integer 3.
Can anyone guide me with the list of integer associated with motionevent in android.
It's not clear if anyone actually answered this question but regardless of the pedantry of whether it is a non-existant mouseevent or a MotionEvent #Satyam asked how to get the name of the action not its value!! Instead of criticizing the questioner, or highlighting his knowledge gap, for not reading. it behoves the answerers to actually read the question. This should be used:
MotionEvent ev
me.actionToString (me.getAction());
It is very useful when logging an app's progress while debugging. I wish there was a similar method for the DragEvent but I have to take a longer path...
You can put code like,
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
//code here....
break;
case MotionEvent.ACTION_UP:
//code here....
break;
case MotionEvent.ACTION_MOVE:
//code here....
break;
case MotionEvent.ACTION_CANCEL:
//code here....
break;
}
Checkout this link
http://developer.android.com/reference/android/view/MotionEvent.html#ACTION_CANCEL
MotionEvent class have all listed event for finger touch
There's no such thing as mouse event on Android. You probably meant MotionEvents. You have the description and explanation of each action in the official SDK documentation (which btw you should read before asking).
`
private static String getActionName(MotionEvent event) {
try {
for (Field f : MotionEvent.class.getFields()) {
if (f.getName().startsWith("ACTION_") &&
f.getInt(null) == event.getAction()) {
return f.getName();
}
}
}
catch (IllegalAccessException ignored) {}
return event.getAction()+"";
}
`
I have an onKeyDown event which wont recognise the first key press (wont even enter the event, i have tested by producing a 'toast' output). On the second key press and after, it works perfectly. If I click on another element on the screen and try the key press again, it yet again needs another key press to get it going. Here is the code:
public boolean onKeyDown(int keyCode, KeyEvent event)
{
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_LEFT:
scorered.performClick();
return true;
case KeyEvent.KEYCODE_1:
red_m1.performClick();
return true;
case KeyEvent.KEYCODE_DPAD_RIGHT:
scoreblue.performClick();
return true;
case KeyEvent.KEYCODE_2:
blue_m1.performClick();
return true;
case KeyEvent.KEYCODE_BACK:
finish();
return true;
}
return true;
}
I have been stumped for hours so any help is much appreciated!
I'm sure, there are good reasons for such behaviour, but do not think, that removing focus is a good solution. My workaround is to fire a keydown event, that "activate" regular onKeyDown functionality. Here is snippet:
new Thread(new Runnable() {
#Override
public void run() {
Instrumentation inst = new Instrumentation();
inst.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_UP);
}
}).start();
To fix this, a work-around is to remove the view focus before pressing any DPAD key. It works in my case. I am having exactly the same issue: When a view of Android Activity is on focus, the very first DPAD key event, that is, the KeyDown event, is ignored: None of these methods are called: onUserInteraction(), dispatchKeyEvent(), onKeyDown(). However, subsequent DPAD key events - KeyUp, KeyDown, KeyUp, ..., can be captured.
Note that this issue does not occur with soft keys (Home, Previous, Recents) nor with hard button keys (BUTTON_A, BUTTON_B, BUTTON_X, BUTTON_Y).