581 Tutorial 4

<< Back to the 581 tutorial overview page

Simulating the Pressure Sensitive Keyboard (PSK)

This tutorial shows you how to simulate the PSK via a normal keyboard, where pressure is generated using a random number generator.

If you want to develop code but don't have the keyboard handy (e.g., if you are sharing a keyboard), you can simulate the pressure sensitive keyboard with your own normal keyboard by doing the following. It interoperates with existing code, so if you just make the few changes and add the event handler as described below, the same programs should work (albeit with a simulated pressure value).

Step 1: Attach an event handler to your top-level window.

Select the top-level window in the designer so its properties are visible in the properties pane. Then click events (the lightning bolt), and go to the KeyDown even. Click and return in the box, which should create an event handler called Window_KeyDown.

Step 2: Make the PSK virtual event handlers accessible to your code

Go into the PressureKeyboard.cs file and change the following two methods from 'protected' to 'public':

  • public virtual void OnKeyPressed(KeyPressedEventArgs e)
  • public virtual void OnKeyReleased(KeyPressedEventArgs e)

Step 3: Have your normal keyboard generate PSK events whenever you press a key.

In your MainWindow.cs file, add the following code.

 //This callback to a normal key press on a normal keyboard will 
 //create a data structure used by event handler of the pressure keyboard
 //by filling in the normal key values, and then generate a random number 
 //representing the 0 - 255 pressure range) and fill in the Pressure value with that.
 //Finally it will generate an OnKeyPressed event, which will completely simulate 
 //an event as if it were fired from the actual pressure keyboard.
 private void Window_KeyDown(object sender, KeyEventArgs e)
 {
     Random random = new Random(); // To generate a random number
     PSK.Key key = new PSK.Key();

     key.Name = "Key" + e.Key.ToString();
     key.Pressure = random.Next (255);
     key.Text = e.Key.ToString();

     this.pressKeyboard.OnKeyPressed(new KeyPressedEventArgs(key));
 }

Step 4: Do the same for the KeyUp event (if desired).

Repeat this, changing the event handler names of course, to simulate KeyUp it.

Step 5. Other things you can do.

Instead of using the random number generator, you can, of course, use any function you wish to simulate pressure. For example, you can put a bit in there that checks if the same key is being repeated, and if so, it increases the amount of pressure reported.

Thanks to Nic for figuring out how to do this.