A simple app to test the KeypadUWPLib. Implements a keypad as part of the app, which when pressed actions the keypad events. Some limitations imposed by lack of KeyUp event with XAML Buttons.

 

image

 

The app presents as an array (as XAML Grid) of Xaml command Button to synthesize a Phone Keypad. The idea was to configure the buttons’ Keydown, KeyUp and KeyHolding events to invoke the corresponding Keypad class events. “Of best laid plans of mice and men!”  This would have been easy with Windows Form. Those three events are trappable for a Command Button. Not so with an Xaml command Button when you use a mouse. To use a mouse with a Button, you would typically trap the button Click event. This essentially is just a response to the KeyDown* event. The Button KeyDown event handler does not fire, nor do the KeyUp and  KeyHolding event handlers with a mouse.

* Note that  the Button Click event; can actually be configured to be for one of KeyDown, KeyUp or KeyHolding Button events..

 

Two approaches have been implemented to get around this issue. Both provide a mechanism for testing the Keypad events from the “soft” keypad.

  • For what follows each button is coded in Xaml as (replace the Tag and Content by each button’s value 0 to 9 , # or *):
            <Button Grid.Row="0" Grid.Column="0" Content="1" Width="45" Height="45" 
Background="Black" Foreground="White"
KeyDown="Button_KeyDown"
KeyUp="Button_KeyUp"
Holding="Button_Holding" Tag="1"
Tapped="Button_Tapped"/>

 

Mouse and PC Keyboard Version

With the  Xaml app running on a desk top and a bit of kludge coding you can get the three events to work, sort of.

With the Button event handlers as follows you can exercise the events thus:

  • In the program settings define IS_PC and rebuild/deploy.
  • Each button is defined in the Xaml code as:Click on the key you want
  1. Press any keyboard key, you get the Keydown event.
  2. Release the keyboard key, you get the KeyUo event.
  3. Press and hold the keyboard key, you get one KeyHolding event...and the Keyup event when released.

This way you can test the Keypad class with software only.

 

When a key is pressed in this roundabout way, the system actions a KeyDown event for the button. When you release it, is this manner, you get a KeyUp event. If you hold it down in this manner, you get a stream of periodic KeyDown events from the system. The code, for when IS_PC is defined, has a state machine which differentiates between the first KeyDown (for whcih  KeyPad class KeyDown event is raised), and “subsequent“ KeyDown system events, until a system KeyUp event occurs. All of the “”subsequent” system KeyDown events for a button are interpreted as one KeyHolding event for the Keypad class.

View the code to see the detail.

 

Touch Version

Xaml is more oriented to touch gestures rather than mouse clicks so with a touch screen as with a Windows 10 phone we can do a little better.

Start by removing the IS_PC define as the MainPage class code differentiates at compile time between these two modes using IS_PC..

        private void Button_Holding(object sender, HoldingRoutedEventArgs e)
        {
            Button key = (Button)sender;
            string face = (string)key.Tag;
            if (e.HoldingState == Windows.UI.Input.HoldingState.Started)
                Keypad.RaiseEvent(this, new KeypadUWPLib.KeypadEventArgs('@', face[0]));
            else
                Keypad.RaiseEvent(this, new KeypadUWPLib.KeypadEventArgs('-', face[0]));

        }

        private void Button_Tapped(object sender, TappedRoutedEventArgs e)
        {
            Button key = (Button)sender;
            string face = (string)key.Tag;
            Keypad.RaiseEvent(this, new KeypadUWPLib.KeypadEventArgs('+', face[0]));
        }

The event handlers used for touch mode.

 

  • If you tap on a button the Tapped event invokes the Keypad KeyDown event.
  • If you tap and hold a button, the Keypad Holding event is invoked.
    - The KeyUp event is invoked when the key is released.