Keypress on xamarin form not entry - android

I have a Xamarin Android form (content page) and I want when the user presses the a certain keyboard key to take a certain action, like calling a method. I can not find anywhere any solution to that.
If there is anyone knowing how to do this I would really appreciate his/her help.
Thank you

Try overriding DispatchKeyEvent in your MainActivity
public override bool DispatchKeyEvent(KeyEvent e)
{
// if the Keycode is A .. there's lots of these
//you'll have to checkout the Keycode class for more options
//this example will fire the following code when 'A' is pressed
if (e.KeyCode == Keycode.A)
{
//invoke your method here
}
//return base if needed, otherwise keystrokes won't get automatically passed into textboxes etc and stuff will act wacky
return base.DispatchKeyEvent(e);
}
https://learn.microsoft.com/en-us/dotnet/api/android.app.activity.dispatchkeyevent?view=xamarin-android-sdk-9
if this doesn't work you can also override OnKeyDown and OnKeyUp and OnKey AFAIK
Difference between onKey(), OnKeyDown() and dispatchKeyEvent() methods provided by Android?

Related

Android: onKeyUp() event is fired even though the focus is on an EditText

Following is what documentation says about Activity.onKeyUp()
Called when a key was released and not handled by any of the views
inside of the activity. So, for example, key presses while the cursor
is inside a TextView will not trigger the event (unless it is a
navigation to another object) because TextView handles its own key
presses.
But, if I override onKeyUp() of an activity and check whether it gets fired when I type a key when the focus is in an EditText, I can see that onKeyUp() is getting fired in following scenarios (tested on an AVD).
If a key is pressed via the hardware keyboard.
If a numeric key is entered from virtual keyboard (event is not fired for character keys).
If I understood the documentation correctly, onKeyUp() shouldn't get fired in above cases.
So, is the documentation wrong, or have I misunderstood it?
Is there any other way I can catch keyboard events which are not handled by any other UI element?
This does appear to be a bug because onKeyDown rightly isn't called if an EditText has focus. However that does mean we can add a hack to figure out if we should react to an onKeyUp event. Here's how I've dealt with it:
private val filteredEvents = HashMap<Long, KeyEvent>()
override fun onKeyUp(keyCode: Int, event: KeyEvent): Boolean {
filteredEvents.remove(event.downTime)?.let {
Log.d("TAG", "Filtered event occurred. Handle it here.")
}
return super.onKeyUp(keyCode, event)
}
override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean =
super.onKeyDown(keyCode, event.also { filteredEvents[it.downTime] = it })

LibGdx: Move camera with Keyboard visible, capture Android BACK button

I have Actors that I need to move when the keyboard becomes visible (When I press a TextField), or they are stuck behind it. I do this by moving the camera up:
stage.getViewport().getCamera().position.set(stage.getWidth()/2, stage.getHeight()/3, 0);
stage.getViewport().getCamera().update();
This works fine. It also works fine to move it back when I touch something outside the TextField and call stage.unfocusAll();
My problem is, when I'm in a TextField and press Androids Back button, it hides the keyboard, but does not call the code I have inside the Inputprocessor (THIS CODE CAPTURES BACK-BUTTON ALWAYS EXCEPT WHEN INSIDE A TEXTFIELD AND KEYBOARD IS VISIBLE):
InputProcessor backProcessor = new InputAdapter() {
#Override
public boolean keyDown(int keycode) {
if ((keycode == Input.Keys.ESCAPE) || (keycode == Input.Keys.BACK) )
{
moveBack();
}
return false;
}
};
I looked around and read that it is not possible to catch the back-button when inside a TextField. Which leads me to my questions:
This must be a common scenario (moving UI to work with keyboard), how do other people do it?
If other people do like me (move camera), how do you handle the Android back-button?
EDIT: This answer captures the back-key while inside a TextField, however it has to be done in the Android Launcher, so I can't reach the elements I need to reach. I also overwrites all other calls to the BACK-button from inside the LibGdx project.
Ok so with the help of This I got a solution working, but it's not very pretty.
Basically I rewrite my GDX Launcher class, and use the following code as the layout:
RelativeLayout layout = new RelativeLayout(this) {
#Override
public boolean dispatchKeyEventPreIme(KeyEvent event) {
if(event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
game.setMoveBack(true); }
}
return super.dispatchKeyEventPreIme(event);
}
};
Then, in the render-method of my MainScreen (Screen that all other Screens inherit from), inside the render-method I call:
if(game.isMoveBack()){
stage.getViewport().getCamera().position.set(stage.getWidth()/2, stage.getHeight()/2, 0);
stage.getViewport().getCamera().update();
game.setMoveBack(false);
}
If anyone has an easier way to do this please post and Ill accept that as an answer, but for now, this works if anyone finds themselves where I am :)

Android onKeyDown() not execute on pressing delete button

I use the following method to delete values from EditText when user presses the delete button of an android device.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DEL) {
onDeleteKey();
return true;
}
return super.onKeyDown(keyCode, event);
}
private void onDeleteKey() {
if (edt_passcode1.isFocused()) {
} else if (edt_passcode2.isFocused()) {
edt_passcode1.requestFocus();
edt_passcode1.setText("");
} else if (edt_passcode3.isFocused()) {
edt_passcode2.requestFocus();
edt_passcode2.setText("");
} else if (edt_passcode4.isFocused()) {
edt_passcode3.requestFocus();
edt_passcode3.setText("");
}
}
I want to delete the previous EditText value when user presses the delete button on their device, but it's not called when the delete button is pressed. But onKeyDown() method doesn't called.
Please help me about it.
Thanks.
From the docs of activity:
Called when a key was pressed down and not handled by any of the views
inside of the activity. So, for example, key presses while the cursor
is inside a TextView will not trigger the event (unless it is a
navigation to another object) because TextView handles its own key
presses.
Im guessing thats your problem?
Link:
http://developer.android.com/reference/android/app/Activity.html#onKeyDown%28int,%20android.view.KeyEvent%29
EDIT:
One possible solution would be to attach a custom onKeyListener to your TextView. It is not necessary to subclass TexView like a commenter suggested, although thats an option as well.
You can get a template for the keyListener from this SO-Question:
Get the key pressed in an EditText
NEXT EDIT:
I just realized the first solution will only work for hardware keyboards.
If you want Virtual Keyboard Support, my best guess wild be an TextWatcher.
See a Sample implementation here:
Validating edittext in Android
NEXT AND HOPEFULLY FINAL EDIT:
Some googling turned up Activity's dispatchKeyEvent() - method. just implement it like you did with onKeyDown, you will be passed a KeyEvent that will tell you all about the pressed key.
Here's a link to the docs:
http://developer.android.com/reference/android/app/Activity.html#dispatchKeyEvent%28android.view.KeyEvent%29

Android avd arrow keys not working

I am learning app development from a book, and have run into a problem with the avd. I have my code set up to change some text when the right arrow key is pressed:
public boolean onKeyDown(int keyCode, KeyEvent event){
if(keyCode==KeyEvent.KEYCODE_DPAD_RIGHT){
textUpdate();
return true;
}
return false;
}
When I run the virtual device and press the arrow keys nothing happens. When I press the arrow keys on my physical keyboard, still nothing. I have done a lot of research and can't find the solution. I have tried editing the avd settings and editing the device(Nexus One) itself to accept keyboard and dpad input. What should I try now?
Have you overriden the method?
There should be an #Override above that function.
Override calls that function whenever its triggered so you can modify it to your specific implementation.
Hope this helps
the "arrow key" means the back key on android device?
if it is then you should override the onBackPressed
#Override
public void onBackPressed() {
// do something
}
Alright, what happened was my onKeyDown() function was inside a Fragment. I don't know why, but when I moved this code into the mainActivity it worked fine. If anyone has an answer as to why this worked and how I could get it to work in the fragment please comment.
Thanks

Android: Is there any way to detect any user input?

on Android! I need to get user input (touch event, keyboard event). is there any way? In java code, It seems there is no way. What about native code?
In java code, It seems there is no way.
=> sorry, there is a way to do detect any action user made and play with application.
Some examples:
KeyboardView.OnKeyboardActionListener
Responding to Touch Events
for touch you can use
mView.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
//show dialog here
return false;
}
});
if your YourActivity implements OnTouchListener you can get the event and where the user touched on the screen:
public class YourActivity extends Activity implements OnTouchListener {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
System.out.println("I touched: "+event.getX()+"-"+event.getY());
}
}
Actually I don't know what you exactly mean. Do you mean text input on an EditText or in General when something is touched?
If you mean text you can use TextWatcher
http://developer.android.com/reference/android/text/TextWatcher.html
If you mean on a View directly you can use OnTouchListener as mentioned above.
https://developer.android.com/reference/android/app/Activity.html#onUserInteraction()
Activity
public void onUserInteraction ()
Added in API level 3 Called whenever a key, touch, or trackball event
is dispatched to the activity. Implement this method if you wish to
know that the user has interacted with the device in some way while
your activity is running. This callback and onUserLeaveHint() are
intended to help activities manage status bar notifications
intelligently; specifically, for helping activities determine the
proper time to cancel a notfication.
All calls to your activity's onUserLeaveHint() callback will be
accompanied by calls to onUserInteraction(). This ensures that your
activity will be told of relevant user activity such as pulling down
the notification pane and touching an item there.
Note that this callback will be invoked for the touch down action that
begins a touch gesture, but may not be invoked for the touch-moved and
touch-up actions that follow.

Categories

Resources