Reputation: 162
I am writing an Android App using Xamarin.Android. I have to detect the hardware keyboard buttons (since my target device is a flip-phone), but I don't know how to do it in Xamarin.
In java it was something like this:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch(keyCode)
{
case KeyEvent.KEYCODE_VOLUME_UP:
Toast.makeText(this, "Volume Up pressed", Toast.LENGTH_SHORT).show();
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
Toast.makeText(this, "Volume Down pressed", Toast.LENGTH_SHORT).show();
return true;
}
return super.onKeyDown(keyCode, event);
}
Is there any way to detect/intercept keyboard buttons? some code example please?
Thank you all!
EDIT
I am looking for something that will allow me to have access to every single hardware button of the keyboard (1,2,3,4,5,6,7,8,9,0,#,*, call btn, endCall btn, etc.)
SECOND EDIT
Found what might be the problem...the code works perfectly, but since my app is a Launcher when I press number keys the dialer starts, any idea on how redirect the dialing to an activity I create?
Upvotes: 3
Views: 4677
Reputation: 162
This is my final solution to intercept numpad keys on android devices. I hope this will help somebody else in the future.
[Activity(Label = "PhoneTask", LaunchMode = Android.Content.PM.LaunchMode.SingleTop)]
[IntentFilter(new[] { Intent.ActionDial, Intent.ActionCallButton, Intent.ActionView },
Categories = new[] { Intent.CategoryDefault },
DataScheme = "tel")]
Just added this piece of code above my activity class and now, when I press these buttons on a laucher app, I am redirected to my activity.
Upvotes: 0
Reputation: 899
Put this method in your activity:
public override bool OnKeyDown (Keycode keyCode, KeyEvent e)
{
switch (keyCode) {
case Keycode.VolumeUp:
Toast.MakeText (this, "Volume Up pressed", ToastLength.Long).Show ();
return true;
break;
case Keycode.VolumeDown:
Toast.MakeText (this, "Volume Down pressed", ToastLength.Long).Show ();
return true;
break;
}
return base.OnKeyDown (keyCode, e);
}
Upvotes: 1