Grabbing Hardware Key Events in Tizen

Jay Cho

Engineer

It's not common to grab a hardware key event in your application, because you can simply use controls such as Editor or Entry when you need to get input from users. However, if you want to make a more advanced application that handles hardware key inputs in more detail or when you want to customize the Smart TV remote control key event, you want to grab hardware key events.

How to get hardware key event

Using EcoreEvent class

There is a class defined in ElmSharp called EcoreEvent. You can use this ElmSharp.EcoreEvent<EcoreEventType> class to create the following ecore event types to be notified.

  • KeyDown
  • KeyUp
  • MouseButtonDown
  • MouseButtonUp
  • MouseButtonCancel
  • MouseMove
  • MouseWheel
  • MouseIn
  • MouseOut

Implementing in the application

Mainpage.xaml

This example shows the preparation in an application's main page to show which key event has occurred.

The first keyDownLabel shows which key down event occurred, and the next keyDownLabel shows which key up event occurred.

<c:CirclePage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:c="clr-namespace:Tizen.Wearable.CircularUI.Forms;assembly=Tizen.Wearable.CircularUI.Forms"
             x:Class="HandleHWKey.MainPage">
  <c:CirclePage.Content>
    <StackLayout VerticalOptions="CenterAndExpand">
      <Label x:Name="keyDownLabel"
          Text="Which key is down?"
          HorizontalOptions="CenterAndExpand" />

      <Label x:Name="keyDownLabel"
          Text="Which key is up?"
          HorizontalOptions="CenterAndExpand" />
        </StackLayout>
  </c:CirclePage.Content>
</c:CirclePage>

Mainpage.xaml.cs

In the cs file, declare and create EcoreEvent with the proper EcoreKeyEventType, as shown below.

Here I created EcoreEventType.KeyDown and EcoreEventType.KeyUp to grab the hardware key down and up events. After creating them, I added event handlers using the On event handler. Both event handlers update the hardware key name and the key code on each label.

EcoreKeyEventArgs is an EventArgs to record the Ecore event's key name and key code. You can see the defined and supported hardware key name here.

using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using Tizen.Wearable.CircularUI.Forms;
using ElmSharp;   // declare using statement

namespace HandleHWKey
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class MainPage : CirclePage
    {
        EcoreEvent<EcoreKeyEventArgs> _ecoreKeyDown;
        EcoreEvent<EcoreKeyEventArgs> _ecoreKeyUp;

        public MainPage()
        {
            InitializeComponent();

            _ecoreKeyDown = new EcoreEvent<EcoreKeyEventArgs>(EcoreEventType.KeyDown, EcoreKeyEventArgs.Create);
            _ecoreKeyDown.On += _ecoreKeyDown_On;

            _ecoreKeyUp = new EcoreEvent<EcoreKeyEventArgs>(EcoreEventType.KeyUp, EcoreKeyEventArgs.Create);
            _ecoreKeyUp.On += _ecoreKeyUp_On;
        }

        private void _ecoreKeyDown_On(object sender, EcoreKeyEventArgs e)
        {
            keyDownLabel.Text =  $"{e.KeyName} ({e.KeyCode})";
        }

        private void _ecoreKeyUp_On(object sender, EcoreKeyEventArgs e)
        {
            keyUpLabel.Text = $"{e.KeyName} ({e.KeyCode})";
        }
    }
}

Running the application on an emulator

You can see the pressed hardware key name and a key code on the upper label, and the released hardware key name and a key code on the lower label.

For TV developers

If you are developing for Samsung Smart TV and want to handle hardware key events, Tizen.TV.UIControls is here for you. Tizen.TV.UIControls provides a variety sets of UI controls for easily creating TV applications and has Xamarin.Forms extension features that help you create TV applications easily.

The InputEvents feature in this library is the wrapper for this ElmSharp EcoreEvents so you can easily grab keys, such as remote control keys, in your application. Check out the guides and the API reference for InputEvents.

You may never need to grab a hardware key event in your application, but when you do, this blog provides the steps you need.