KeyEvent handling in android? - android

I want to programattically handle the ENTER key in android.
Is there any way to achieve this?
Please Help!!
Thanks in advance

You need to carefully override the following function:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_ENTER:
{
//your Action code
return true;
}
}
return super.onKeyDown(keyCode, event);
}

Use onKey() to capture the key. KEYCODE_NUMPAD_ENTER is used for the numeric ENTER key, and KEYCODE_DPAD_CENTER is the Directional Pad Center key.

Related

WebView onKeyListener not working

I have to intercept the touch on the screen, and this is my code:
mWebView = findViewById(R.id.webview);
mWebView.setWebViewClient(new WebViewClient());
mWebView.loadUrl(URL);
mWebView.setOnTouchListener(this);
mWebView.setOnKeyListener(this);
and my listeners:
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
Log.i(TAG, "Hello, touch.");
onTouchEvent();
return false;
}
return false;
}
#Override
public boolean onKey(View view, int i, KeyEvent keyEvent)
{
Log.i(TAG, "Hello, onKey.");
onTouchEvent();
return false;
}
While the on touch is working fine for touches on the screen, it dosen't intercept the touch event on the keyboard. For this reason, I've added the onkeylistener, but it's not intercepting the key events. As last thing, I don't care about what is typed, but just if the screen has been touched.
why not trying onkeyDown?
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(webView.canGoBack()){
webView.goBack();
}else{
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
OR
override this method --> onUnhandledKeyEvent (WebView view,
KeyEvent event)
more details on:
https://developer.android.com/reference/android/webkit/WebViewClient.html#onUnhandledKeyEvent(android.webkit.WebView,%20android.view.KeyEvent)
OR
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
boolean dispatchFirst = super.dispatchKeyEvent(event);
// Listening here for whatever key events you need
if (event.getAction() == KeyEvent.ACTION_UP)
switch (event.getKeyCode()) {
case KeyEvent.KEYCODE_SPACE:
case KeyEvent.KEYCODE_ENTER:
// e.g. get space and enter events here
break;
}
return dispatchFirst;
}
NOTE:-
Preventing developers from accessing the events by default was made by Googlers on purpose. Because the key event input isn't the only one anymore. There're gestures, voice and more is coming. Official recommendation is to "stop relying on legacy key events for text entry at all". Check out more details here: https://code.google.com/p/android/issues/detail?id=42904#c15
With my recent experience of inexplicable and inconsistent behaviour of calling loadUrl() before setWebViewClient() I would try calling loadUrl() AFTER setting ANYTHING to do with a multi-threaded WebView. That includes setOnTouchListener() and setOnKeyListener().
I realise my answer might not help your specific problem - but it might help others who have experienced similar behaviour and are setting things before before calling loadUrl().
If you want to detect keypress then try to override
onKeyPreIme(int keyCode, KeyEvent event);

Android: catching action for keyboard enter button being pressed

I am trying to respond to pressing the enter button on a standard system on screen soft keybord, in numeric mode. keyboard appears on the screen when myCustomEditText class is given focus.
tried 2 approaches:
overriding onKeyDown():
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_ENTER) {
//do sth..
return true;
}
return super.onKeyDown(keyCode, event);
}
setting listener setOnKeyListener:
myCustomEditText.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_ENTER) {
onRightButtonClicked();
return true;
}
return false;
}
});
(also tried onKeyDown() in myCustomEditText as well, but with no effect)
problem is:
these aproaches above works for all the keys being pressed but for the enter key - the methods are not even being fired at all. (it's not a problem of bad KeyEvent.KEYCODE_...)
my question is:
why these methods are not being fired for enter key, but are for other keys, and how to listen to enter key being pressed properly?
You respond to ENTER differently than normal keys, try something like this:
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
switch (actionId) {
case EditorInfo.IME_ACTION_DONE:
...
return true;
default:
return false;
}
}
});
You can specify which action is performed by ENTER by specifiying android:imeOptions in XML or by calling editText.setImeOptions(...) with one of the constants in EditorInfo. imeOptions also changes how the ENTER key looks! For example if you set imeOptions to actionSearch then the ENTER key will look like a search button.
You can find a list of all possible values for imeOptions along with a description here.

Disable DPAD keys in android

I am trying to catch events generated by the arrow keys (UP, DOWN, RIGHT and LEFT) and disable them. Below code snippet is from one of the activity class.
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.KEYCODE_DPAD_DOWN) return true;
else return true;
}
However, with those code in place, key navigation is working. I tried adding key listener to activity which doesn't work either.
The target device is Samsung GT-I5500 with Android 2.2 version on.
Am I missing anything?
Override onKeyDown also and return true and not false.
Somnething like this:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_LEFT:
case KeyEvent.KEYCODE_DPAD_RIGHT:
case KeyEvent.KEYCODE_DPAD_UP:
case KeyEvent.KEYCODE_DPAD_DOWN:
return true;
}
return false;
}
In the documentation, it is stated that you should return:
true if you handled the event
false if if you want to allow the event to be handled by the next receiver.
Your method is returning false, so you are passing the event to the default key handler

How to track the down arrow button in virtual keyboard?

In the above figure i want to listen for down arrow( i have marked it in red colour, the key one which is used to hide the keyboard). I have tried to get it action through menu options, viewconfiguration class etc. But it is not useful. Is there any method, listeners or any callback event to track this button. Please let me know your suggestions and thoughts. Any help in this regard is highly appreciable
Thanks inAdvance
In your activity, override this method: http://developer.android.com/reference/android/app/Activity.html#onBackPressed()
If I am not you talking about this method
public boolean onKeyDown(int keyCode, KeyEvent event) {
super.onKeyDown(keyCode, event);
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
Toast.makeText(KeyActions.this, "Pressed Back Button", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}

Android listen for backbutton longclick

I am overriding the backbutton with a onBackPressed() function
how do I also detect long clicks on the backbutton? Is there an equivalent of #Override onBackLongPressed() ?
This might help you (Check the first comment) - Android long key press
From Android 2.0, Activity contains the method
public boolean onKeyLongPress(int keyCode, KeyEvent event)
For exemple, a long key press on the back button would be :
#Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK)
{
// do your stuff here
return true;
}
return super.onKeyLongPress(keyCode, event);
}
Check "Story 2" here. There's not a shortcut for it like there is onBackPressed().
I think you'll have to use onKeyLongPress and handle the KEYCODE_BACK event yourself.

Categories

Resources