Abstracts the Keypressed event further by interpreting it in terms of the actual key press. Has a delegate for each key which can be programmatically set by the dependant app as the function (implemented in the app) to call when each each key is pressed. Concepotually, sits on top of KeypadUWPLib.
The KeyFunctions Class
Each key has one delegate which is set from the main app using the Set( ) method passing the relevant main app’s function and the key’s character.
The Keypresses field is a Dictionary of Keypress delegates for the keys. Each delegate is referenced by the character of its key (0..9, # and *).
KeypadUWPLib.Keypad Keypad = null; public delegate void Keypress(); Dictionary<char, Keypress> Keypresses = null;
The class fields
The class constructor instantiates the dictionary and populates it with a null delegate for each key. The null reference is set in the Set( ) method. The constructor takes as input a reference to an instance of the Keypad class, as the Keypad field, which typically will be configured to handle a physical phone keypad, such as the Bluetooth Arduino Keypad through the BluetoothSerial class.
public KeyFunctions(KeypadUWPLib.Keypad keypad) { Keypresses = new Dictionary<char, Keypress>(); foreach (char ch in KeypadUWPLib.KeypadEventArgs.ValidKeys) { Keypresses.Add(ch, null); } Keypad = keypad; Keypad.KeyDown += Keypad_KeyDown; }
The class captures the KeyDown event for the Keypad in the Keypad_KeyDown event handler for the Keypad. of the Keypad class. It determines which key is the source and invokes it delegate in the dictionary.
private void Keypad_KeyDown(object sender, KeypadUWPLib.KeypadEventArgs e) { char cmd = e.Key; Action(cmd); } public void Action(char ch) { if (Keypresses[ch] != null) { Keypress del = Keypresses[ch]; //Get the delgate fron the dictionary del(); //This calls the delegate } }
A delegate can be removed from the dictionary using Clear( ) and is explicitly invoked using the Action( ) method (as above).
public Delegate Get( char ch) { return Keypresses[ch]; } public void Clear(char ch) { Keypresses[ch] = null; }
The Get( ) and Clear( ) methods
The BTKeypadUWPApp2 project implements a variation of the BTKeypadUWPApp application that exercises the KeyFunctions class. The main app calls SetupDelegates(); to set up the delegates, one for each key:
private void SetupDelegates() { keyFunctions = new KeyPadKeysUWPLib.KeyFunctions(SerialPort.Keypad); keyFunctions.Set(HashKey, '#'); keyFunctions.Set(StarKey, '*'); keyFunctions.Set(Num0, '0'); keyFunctions.Set(Num1, '1'); .... ..... keyFunctions.Set(Num8, '8'); keyFunctions.Set(Num9, '9'); }
Note that this method instantiates the KePadKeys class pass to it an instance of the Keypad.
The app then implements a simple delegate for each such as:
public void StarKey() { System.Diagnostics.Debug.WriteLine("StarKey invoked"); } private void Num0() { System.Diagnostics.Debug.WriteLine("Num0 invoked"); }
The test app and its subprojects.
This is the last in this series of blogs. The final outcome of this activity is though a Selfy Stick; watch this blog for further information. The intention is to use these APIs with an Arduino connected phone keypad to control a major UI based app running on a phone or IoT-Core device. For a Selfy Stick that will be a custom camera app on the phone (already done that part). Further variations will be a TCPIP sockets version and finally an Azure IoT Suite version.
PS: I will still make some updates to these blogs.