Disable DPAD keys in android - 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

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);

I am not getting the event when long press on power button on android 6.0

I have implemented onKeyDown function in my activity and i want to open a dialog if we long press on power button. I have implemented that function as per following manner and working fine on older versions of android but isn't working on android 6.0
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch(keyCode){
case KeyEvent.KEYCODE_POWER :
if(event.isLongPress()){
//Do something
return true;
}
break;
default :
break;
}
return super.onKeyDown(keyCode, event) ;
}
Can you please help me.

How to use menu and onKeyDown() on the same activity

I am developing an Android app. I have written my own code for back key event.
For that i am catching events using this method
public boolean onKeyDown(int keyCode, KeyEvent event) .....
I also want to use Menu options I have used Inflator for that.
But when I click on menu button, that event caught by my onKeyDown method.
I am not expecting that, I expect to execute following methods,
public boolean onCreateOptionsMenu(Menu menu){ // creating menu using MenuInflator }
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case :
return true;
default:
}
}
How could I use both the things on the same activity?? Any Idea ???
According to the documentation of onKeyDown():
it is normal that it receives the 'menu button pressed' event, as any KeyEvent
but if you return false, this event will continue to be propagated and will therefore lead onOptionsItemSelected() to be called
So you should code your onKeyDown() in a way that lets the KeyEvent to propagate if you do not handle it on your own.
In the following example, we have two keys that are interesting to us: one we fully handle on our own, one we just track, and we ignore all the rest:
protected boolean onKeyDown(int keyCode, KeyEvent event) {
boolean handled = false; // By default we let the event propagation turned on
if (keyCode == A_KEY_YOU_WOULD_LIKE_TO_HANDLE_BY_YOURSELF) {
doSomething();
handled = true; // Stops the event propagation
}
else if (keyCode == A_KEY_YOU_WOULD_LIKE_TO_TRACK_WITHOUT_PREVENTING_THE_SYSTEM_FROM_HANDLING_IT) {
doSomethingElse();
}
// else, you neither handle or track that key
return handled;
}

Android: register keyboard action?

Is there an equivalent of Java's registerKeyboardAction, that allows the handling of a key event regardless of where it occurs?
You can use onKeyDown(int keyCode, KeyEvent event) to handle key events:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_Y:
// do something
break;
case KeyEvent.KEYCODE_N:
// do something
break;
}
return super.onKeyDown(keyCode, event);
}
This listens for key events only on the Activity where it is defined. If you need to handle key events in every section of your app, you'll need to define onKeyDown on every Activity.

Can Menu and onKeyDown work together?

I want to use Menu and OnkeyDown in same activity but it is not working, when I use OnkeyDown then menu disappear from the activity. I want to make a action using virtual keyboard's enter key. When I press enter key of virtual keyboard one toast should be appear.
Please help.
Thanks in advance
You have to return false outside the switch statement of the onKeyDown but true for every key you handle:
public boolean onKeyDown(int KeyCode, KeyEvent event) {
switch (KeyCode) {
case KeyEvent.KEYCODE_CAMERA:
//take photo
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
//lower volume
return true;
case KeyEvent.KEYCODE_VOLUME_UP:
//raise volume
return true;
}
return false;
}
This works for 2.3.3

Categories

Resources