Xamarin Android listens for volume keys

In the first part of the article, OnKeyDown is rewritten in MainActivity, and the volume key is printed and printed. Of course, the physical buttons and virtual keys (volume keys, return keys, menu keys, etc.) can be captured by this button event.

 

However, the keys are rewritten in MainActivity, and the method performed when the volume key is pressed is in other pages.

 

The author came up with an immature idea: using publish-subscribe mode, defining publish in MainActivity, instantiating the post-layer delivery to my page, and connecting the instantiated subscribe. As shown in the figure, the order of class instantiation is left to right:

Scheme realization:

1. Publisher Subscriber Class Design

namespace NetworkAssistant
{
    //Declare keystroke event monitoring delegate interface
    public delegate void OnKeyDown_callback(int keyCode);
    //Key Event Publisher
    public class MyKeyEventPublish
    {
        private int KeyCode;
        public Action<int> OnKeyDown { set; get; }
        public int OnKeyDownChanged
        {
            get { return KeyCode; }
            set
            {
                KeyCode = value;
                OnKeyDown?.Invoke(value);
            }
        }
    }
    //Key Event Subscriber
    public class MyKeyEventSubscribe
    {
        OnKeyDown_callback cb;
        public MyKeyEventSubscribe(OnKeyDown_callback _cb)
        {
            cb = _cb;
        }

        public void OnKeyDown( int newKeyCode)
        {
            Console.WriteLine($"[{newKeyCode}]");
            cb?.Invoke(newKeyCode);
        }
    }
}

 

2,MainActivity.cs

//Key Event Publisher
MyKeyEventPublish myKeyEventPublish;
protected override void OnCreate(Bundle savedInstanceState)
{
    //Instantiate key event publisher
    myKeyEventPublish = new MyKeyEventPublish();
    TabLayoutResource = Resource.Layout.Tabbar;
    ToolbarResource = Resource.Layout.Toolbar;

    base.OnCreate(savedInstanceState);

    Xamarin.Essentials.Platform.Init(this, savedInstanceState);
    global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
                
    LoadApplication(new App(myKeyEventPublish));
}

public override bool OnKeyDown([GeneratedEnum]Keycode keyCode, KeyEvent e)
{
    //Key Value Change Event Publishing Method
    myKeyEventPublish.OnKeyDown((int)keyCode);

    //switch (keyCode)
    //{
    //    case Keycode.VolumeUp:
    //        Message.ShortAlert("Volume up");
    //        break;
    //    case Keycode.VolumeDown:
    //        Message.ShortAlert("Volume down");
    //        break;
    //    default:break;
    //}

    return true;//The original function of shielding button
    //return base.OnKeyDown(keyCode, e);//Do not shield the original function
}

 

3,App.cs

namespace NetworkAssistant
{
    public partial class App : Application
    {
        public App(MyKeyEventPublish myKeyEventPublish)
        {
            InitializeComponent();

            MainPage = new MainPage(myKeyEventPublish);
        }

        protected override void OnStart()
        {
            // Handle when your app starts
        }

        protected override void OnSleep()
        {
            // Handle when your app sleeps
        }

        protected override void OnResume()
        {
            // Handle when your app resumes
        }
    }
}

 

4,MainPage.cs

//Key Event Publisher
MyKeyEventPublish myKeyEventPublish;
//Key Event Receiver
MyKeyEventSubscribe myKeyEventSubscribe;
public MainPage(MyKeyEventPublish mkp)
{
    //Instantiate key event publisher
    myKeyEventPublish = mkp;
    //Instantiate key event recipient
    myKeyEventSubscribe = new MyKeyEventSubscribe(OnKeyDownCallBack);
    //Add subscription
    myKeyEventPublish.OnKeyDown += myKeyEventSubscribe.OnKeyDown;

    InitializeComponent();
}
void OnKeyDownCallBack(int keyCode)
{
    if (keyCode == 24)
    {//Volume key
        //Message.ShortAlert("Volume key");
        SendData("VU");
    }
    else if (keyCode == 25)
    {//Volume key
        //Message.ShortAlert("Volume key");
        SendData("VD");
    }
    else
    {
        Console.WriteLine("Press the key:"+keyCode);
    }
}

 

Welcome to the discussion. Thank you very much.

Keywords: Android

Added by pyrodude on Thu, 10 Oct 2019 06:42:07 +0300