How can I use onTouchEvent() in softkeyboard? - android

I know that onTouchEvent()
can return the current touch point's x,y in a view,
However, when I touch a key on a softkeyboard.
the method is not working,
My question is how can I apply onTouchEvent to softkeyboard on Android?

It is not possible to get a touch event for a soft keyboard.
If you want to capture keys as they are entered into an EditText, check out the EditText.addTextChangedListener() function.
Alternatively, try implementing the View.OnKeyListener interface in your Activity, or overriding public boolean dispatchKeyEvent(KeyEvent e), or overriding public boolean onKeyDown(int keyCode, KeyEvent event). In all of those cases, you can check what the keyCode or KeyEvent is and handle it as you wish.

Related

What key gets pressed to hide soft keyboard in android

I'm trying to detect when the soft keyboard gets closed, I found this code snippet
#Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK){
//detects that keyboard was hidden
}
return super.onKeyPreIme(keyCode, event);
}
but this needs to be added to a subclass that extends editText, I'm not intrested in implementing the functionality I want to add on keyboard hidden in all my app, I want to add it for just one Activity, I tried onKeyDown, onKeyBack, onKeyUp, onBackPressed nothing seems to log the back press that closes the soft keyboard.
so my question is there a way to detect the click on the button that hides the keybaord?

Block physical keyboard input in EditText

I have a EditText that should not allow the user to input anything through the keyboard (soft or hard). This EditText should only allow the user to input something through keys(buttons) displayed in the screen by the app.
I have disabled the soft keyboard, but I can't find a way to disable the hardware keyboard input. This input via hardware keyboard can be done using a emulator that is configured to allow input through the hardware keyboard.
So, my question is, How can I block the input via physical keyboard in a EditText?
Thank you!
Finally got it!
Code:
editText.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
return true;
}
});
Explanation:
Returning true will tell the listener that I've already handled the hardware keyboard input. Based on Android documentation (http://developer.android.com/reference/android/view/View.OnKeyListener.html)
View.OnKeyListener: Interface definition for a callback to be invoked when a hardware key event is dispatched to this view. The callback will be invoked before the key event is given to the view. This is only useful for hardware keyboards; a software input method has no obligation to trigger this listener.
onKey(View v, int keyCode, KeyEvent event)
Called when a hardware key is dispatched to a view.
You could try these according to what you want in your xml layout
android:longClickable="false"
android:clickable="false"
android:cursorVisible="false"
android:editable="false"
Then make the edittext show what the user typed through the keyboard you made in the app.

How to handle event while clicking H/W OK button on android device

I am using a device having keypad(Hardware) attached on phone device.Now I want to get the event after clicking OK button on keypad.
i found we use DPAD_CENTER for specifiying OK Button.
Can anyone helps me on this. How set listener for this OK button. Thanks
Override onkeydown function and you can do actions for whichever hardware key is pressed
public boolean onKeyDown(int keyCode, KeyEvent event){
if(keyCode==KeyEvent.KEYCODE_DPAD_CENTER){
// Do what you have to do here
}
return false;
}

Android capture hardware keyboard event without edittext view

How can I capture the hardware keyboard events without using an EditText field?
For example, in a simple activity the display a square on the screen, when a "B" is pressed on the slide keyboard I want to turn it blue, when a "G" is presses, turn it Green, etc.
I don't need help with the color code, just how to intercept the keypress
This is not about the soft or virtual keyboard
Android classes usually provide event handlers, you can implement when subclassing them. The Activity class has the following event handlers:
onKeyDown(int keyCode, KeyEvent event)
onKeyLongPress(int keyCode, KeyEvent event)
onKeyMultiple(int keyCode, int repeatCount, KeyEvent event)
onKeyShortcut(int keyCode, KeyEvent event)
onKeyUp(int keyCode, KeyEvent event)
In addition all views have the following event handlers:
onKeyDown(int, KeyEvent)
onKeyUp(int, KeyEvent)
I guess there are many other classes that have similar event handlers for key events, but this should be enough for your situation. The KeyEvent then contains information about the pressed key, i.e. the key code.
Activity class has already implemented KeyEvent.CallBack see here
you just need to override these methods and implements all events that you want

onKeyDown not always called in Android app

I've created a simple android game, based on the Lunar Lander sample, and I'm having a problem with handling key events. When the activity starts, the only keys that onKeyDown or onKeyUp get called for are the dpad up/down/left/right keys. Neither the menu, back, or dpad_center keys trigger onKey methods. However, once I've pushed one of the dpad up/down/left/right buttons, pressing the menu, back, or dpad_center keys do trigger these methods. I'm not getting any errors, or any indication of what's going wrong.
It's possible that the focus is set wrong - the activity is started from a button on screen, so it could be in touchscreen mode. If that's the case, shouldn't touching the back button get me in to the right focus mode so that I can catch the event?
I'm using the emulator from SDK-1.5r3. I have not been able to try this on a real phone yet. Here's my onKeyDown.
public boolean onKeyDown(int keyCode, KeyEvent msg)
{
Log.d(TAG, "onKeyDown: " + keyCode);
return super.onKeyDown(keyCode, msg);
}
Thanks
Matt
Is this onKeyDown in a view or in the activity?
If setContentView is called passing in a view, and that view has setFocusable(true) called on it, all key events will bypass the activity and go straight into the view.
On the other hand, if your onKeyDown is in the view, and you haven't called setContentView on the Activity and setFocusable(true) on the view, then your Activity will get the key events and not the View.
Look for those specific calls but I think you're right about it being a focus issue.
Activity's onKeyDown/onKeyUp methods not always called. Unlike them dispatchKeyEvent on Activity fired always. Move keydown/keyup logic here. Works well.
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
// keydown logic
return true;
}
return false;
}
insert this code
getWindow().getDecorView().setFocusable(true);
if (SDK_INT >= Build.VERSION_CODES.O) {
getWindow().getDecorView().setFocusedByDefault(true);
}

Categories

Resources