My name is Bruno.
Unfortunately I am having a problem with key pressing.
The problem:
When I press any key from the keyboard and keep it pressed, the function onKeyUp() is called
without releasing that key. The correct behavior is: when we press a key, the function onKeyDown() is called and when that key is released, onKeyUp() is called.
I tested some Android emulators and only some of them this problem occurs. I want to use Android Studio Emulator. For example, Bluestacks this problem don't happens. I don't know why this occurs on some emulators.
Here is my code, MainActivity.java:
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
Log.i("KEYTEST", event.toString());
return super.onKeyUp(keyCode, event);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
Log.i("KEYTEST", event.toString());
return super.onKeyDown(keyCode, event);
}
When I press 'D' key and keep it pressed, then log prints:
log image
What is happening? There a problem with certain emulators in combination with portuguese keyboard?
Don't use onKeyUp and onKeyDown for letters. Most keyboards don't use them, they send data via the commitText() function of the InputMethodConnection. That completely bypasses the key up/key down system. Those are only reliable for physical buttons. Like the volume buttons, or a hardware keyboard attached via bluetooth.
Related
I am trying to develop a launcher TV app in android Java. I want the launcher app to show my own custom setting class when settings button is clicked from Dpad. for now when i click the settings button from dpad android system settings is triggered and displayed.
I think this can be achieved because in all the launcher of app of Android Television APP when the settings button is clicked from Dpad their custom setting class is opened rather than system settings.
Can anyone help me how can i acheive that ?
Have you tried overriding onKeyDown?
if not. you can start there.
//Handling remote control actions
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KEYCODE_SETTINGS){// from developers page
// your coder here
// and return true if you don't want system settings to be launched
return true;
}
}
But as I found out on my TV. some keys don't invoke KeyEvent. instead (in mine it was the volume keys) broadcast an action. if that is the case, you have to use broadcastReceiver and get the action from there.
for KeyEvents I ended up having a class that implements KeyEvent.Callback{}
I am trying to build an Android TV application using Android Studio that supports channel up/down buttons as an extra features to navigate to different YouTube channels/shows/videos (like a normal satellite TV would do) rather than only supporting the D-pad navigation buttons. Is it possible to achieve this & hopefully anyone had an idea how to do this?
Thank you.
While waiting for someone to reply to this question, I've discovered the solution myself.
Basically you just need to Override onKeyDown() or onKeyUp() method in related Activity class, and don't forget to return super.onKeyDown() or super.onKeyUp() so unattended KeyEvent will be attended as normally it will.
As for my case, I'm trying to have custom features for channel up & down buttons only. So below is the sample code.
#Override
public boolean onKeyDown(int KeyCode, KeyEvent event){
boolean handled = false;
if(KeyCode == KeyEvent.KEYCODE_CHANNEL_DOWN){
Log.i("KeyEvent","Channel down button pressed");//for debugging, to be printed on logcat
handled=true;
//do something
}
else if(KeyCode == KeyEvent.KEYCODE_CHANNEL_UP){
Log.i("KeyEvent","Channel up button pressed");//for debugging, to be printed on logcat
handled=true;
//do something
}
if(handled){
return handled;
}
//return super.onKeyDown() to attend unattended KeyEvent
else{
return super.onKeyDown(KeyCode, event);
}
}
KeyEvent class documentation that contains list of KEYCODE constants available can be found here.
So first off, I got this snippet of code working in an empty activity, it just calls for every time when I touch the device.
#Override
public boolean onTouchEvent(MotionEvent ev) {
System.out.println("touch");
return mDetector.onTouchEvent(ev) || super.onTouchEvent(ev);
}
But as soon as I apply this snippet of code to an activity which also has a GridViewPager, the listener doesn't report any touch events.
So my guess is, that all touch Events are getting blocked by the GridViewPager and my listener is not getting anything.
Is there a way how I can fix that?
I have a EditText view on my RelativeLayout, and I can capture the soft input keyboard keycode KEYCODE_ENTER (int 66). This is the Key listener:
myedittext.setOnKeyListener(new View.OnKeyListener()
{
#Override
public boolean onKey(View v, int keyCode, KeyEvent event)
{
Log.d("softinput", "key received!");
}
});
The problem is that the soft keyboard displays the key labeled "Next" and I need it to display "Done". When i (successfully) change the key to "Done" using the API:
myedittext.setImeOptions(EditorInfo.IME_ACTION_DONE);
The Key listener stops working and i can no longer receive anything in the key listener. The LogCat doesnt show anything. It literally stops working. When i remove the above line of code, everything works again but the key displays "Next".
I was guessing this was clearing some other flag required for it to notify keys, but using EditorInfo.IME_ACTION_NEXT works perfect, but the soft key, of course displays "Next".
Anybody knows a workaround for this? or the reasons why this happens?
Thanks.
You say you have an EditText, but your variable is "mytextview". Are you setting the key listener correctly to the EditText ?
Change imeOptions to action done in XML rather than in code.
Add the following to the edittext tag,
android:imeOptions="actionDone"
Hi My requirement is like this. I have an activity in that I want to disable back button, menu button, home button screen settings button. When I touch on that screen I open one alert dialog with two buttons. If I click ok button all the buttons are enabled and my app is exit.
Thats my requirement. If anyone knows about this, please reply me as soon as possible. Quick reply is Very very appreciable.
To achieve this you must specify your activity as android.category.HOME.
And when you finish your activity (press ok button) you must start default launcher on phone.
But it's mean that on every home button press you will see your activity. To prevent this you must determine how your activity was started.
I would rather override onKeyDown method
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU) {
//DO NOTHING
}if(keyCode == ..etc){
//Still do nothing
return super.onKeyDown(keyCode, event);
}
This way you will have full control of what happens.
Regards,
Robert