I want to know if there is any provision that allows me to read the input key from a hardware keyboard present on the phone or connected externally in android . I basically am building an application that needs to have special shortcuts set . I researched about the WindowManagerPolicy but eclipse does not seem to support any interface of that sort . I need help even starting .
Thank you
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
return super.onKeyDown(keyCode, event);
}
override activity's onKeyDown to catch keyboard event
Related
In Android application is it possible that when I press my mobile *(star) button(not widget button) then I can perform any particular events in my application? If it's possible, then how may I achieve it?
If you mean the * key from your hardware keyboard ( on the devices that have it) you can capture it using KeyCode.
Here you can find an extensive list of all the keys you can intercept.
To do it:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_STAR: //here you check any key you want
{
//your code here
return true;
}
}
return super.onKeyDown(keyCode, event);
}
EDIT
Answering your comment, I don't believe this is possible. The KeyDown/Up events are handled on Activities. And you won't have an Activity active. Check this out!
EDIT
Yeah, according to this guy you can't.
If the button is within your own app, then yes.
If you mean a button in any other app (I think you mean the * key on the dial pad), then no.
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;
}
I need to override the android heaset hook button, the long press causes the music player starts auntomatically and I need to avoid this.
Is it possible in Android?
I try:
#Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_HEADSETHOOK) {
return true;
}
return false;
}
But it not works.
Thanks,
It is possible to just create your own gesture recognition to account for a long press. You can start a timer on KeyDown and then check that timer on KeyUp to see if the key up was at or lower than your long press time. Or you can use the getEventTime methods to do the same function. If your question was more specific on how to intercept the headset buttons I would recommend this article, Allowing applications to play nice(r) with each other: Handling remote control buttons.
If you need some other kind of help or some code samples let me know
I am trying to port my Android app to work well on tablets with Honeycomb. One issue I am having is when the keyboard is showing and the back button turns into a down arrow, I can't seem to pick it up as not only a back button action, but as a keyevent in general. Here is my code:
#Override public boolean onKeyDown(int keyCode, KeyEvent event) {
Log.d(TAG, "keycode: " + event.getKeyCode());
}
So when the arrow is pointing down, I don't get a LogCat read out, and it appears it doesn't even know the down arrow is a KeyEvent. Has anyone else run into this? How can I fix it?
Look at the View.onKeyPreIme() method.
Although reading your question and your reply to Mark, I'm unsure as to whether you're writing an app or an input method.
I am developing an application that I don't want user to touch home/back button. Trust me, I have a good reason for it.
What I need to do is to disable home/back or even the keyboard using terminal.
I've look through commands in adb shell already and I cannot find any command to fix this.
I'm not sure about AVD, but in real phone this buttons are hard buttons. So, no software can remove them. All you can do is to override the behaviour for this buttons using onKeyDown() method.
Using the following piece of a code in an Activity subclass would work for intercepting key events:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
return true;
// return super.onKeyDown(keyCode, event);
}
The back key is the constant KeyEvent.KEYCODE_BACK; the home button should be similar.