CS 247L: Soldering & electronics

In this tutorial:

  1. Introduction to the IPAC Keyboard Emulator
  2. Project: Make two lights flash with external switches
  3. Key mapping
    1. IPAC to Keyboard
    2. Keyboard to Flash
  4. Turning off key repeat
  5. Pressing multiple buttons at the same time in Flash
  6. Debouncing

Introduction to the IPAC Keyboard Emulator

For part of this lab, we'll be using the Ultimarc I-PAC2, which emulates a USB or PS/2 keyboard. You wire up switches to the I-PAC's screw terminals and connect the board via USB to your computer. The I-PAC then translates switch on/off events into keyboard events. For the attached PC, this input is indistinguishable from a normal keyboard. Thus you can use the I-PAC to drive Flash prototypes or even closed-source software like Windows Media Player.

Project: Make two lights flash with external switches

Wire up one switch between terminals "GND" and "1RGHT", and another switch between terminals "GND" and "2RGHT" on the other side of the circuit board. Here's a sample wiring diagram from Ultimarc (which uses different inputs) - note how there is one daisy-chained connection to GND:

Then download and run cs247-keypress-lights-onoff.swf (fla source). Press the switches - the "lights" in the Flash file should behave as follows: Light turns on when a momentary switch is depressed, off when it is released.

Also try out cs247-keypress-lights-toggle.swf (fla source). Here the lights toggle state between on and off with each press, i.e. you have to press twice to turn a light on and off again.

You can test the Flash file with your regular keyboard - the right arrow key should turn on the red light, while the "g" key should turn on the green light.

Lights not responding as you expect them to? Read more about problems with Flash and multiple key presses .

Key mapping

IPAC-to-Keyboard

The I-PAC was originally designed for people who rebuild arcade games with PC emulators. As such, the inputs have somewhat cryptic labels such as 1RGHT (Player 1 move right) or 1COIN. The table below shows the standard assignment of these codes to keyboard keys:

For example, the switch "P1-SW7" corresponds to key "C". You can change this mapping using the WinIPAC Panel Designer Software (documentation, local download). There is a Mac programming Utility (download) as well, which I have not tested. The IPAC saves custom configurations to EEPROM, so you do not have to reprogram when you unplug and re-plug. I suggest testing your mapping by opening up notepad or a similar text editor and making connections between ground and switch screw terminals with a piece of stripped wire.

Keyboard-to-Flash

Once you are successfully generating keyboard events with the IPAC hardware, you'll probably want to read those events in software. I'll cover how to do this in Flash here. In Flash, you create a keyboard listener object and register it with the Key class:

var keyListener:Object = new Object();
keyListener.onKeyDown = function() {
  trace("Key Pressed");
}

keyListener.onKeyUp = function() {
  trace("Key Released.");
}
Key.addListener(keyListener);

Within the listener, you call Key.getCode to read the value of the key last pressed. The Key class has static attributes that match codes returned by Key.getCode for non-alphabetic keys that don't have a straightforward string representation:

To check if the last key pressed was the up arrow, you'd use:

if (Key.getCode()==Key.UP) {
  //do something
} 

For alphabetic keys, you can use:

if (String.fromCharCode(Key.getAscii()) == "a") {
  //do something
}

Flash-creations.com has a more complete table of ascii and key codes.

Turning off key repeat

One of the more annoying parts of keyboard input you have to deal with is typematic key repeat - when you press a key and keep it pressed, the operating system automatically generates new events while you keep the key pressed. While this feature is useful for normal cursor navigation, it tends to be a source of confusion when programming interaction designs with external switches. Here are two workarounds

1) Turning off key repeat in the operating system (Windows only)

On Windows, you can turn key repeat off through the accessibility options:
http://www.microsoft.com/windowsxp/using/accessibility/filterkeys.mspx
(Control Panel->Accessibility->Choose Keyboard tab->Check "Use
FilterKeys"->Click on Settings next to it->Next panel, select "Ignore
quick keystrokes and ..."->Click on Settings->Next panel, select "No
keyboard repeat").


(click on image for full scale version)

2) Turning off key repeat in Flash

I'm not sure what the steps are to turn repeats off on a Mac, so there's a second option, to circumvent repeats in Actionscript inside Flash: Repeatedly pressing a key with your finger generates the following string of events:

press,release,press,release,press,...

However, pressing a key and just holding it generates a different string of events:

press,press,press,press,....
So we can write a key handler in Actionscript that ignores repeated press events without release events in between. Here's the Actionscript (download cs247-keypress.fla):
   var keyListener:Object = new Object();
   var keyState:Array = new Array(); //associative array to keep track of
   press/release state for each key
   
   keyListener.onKeyDown = function () {
        //deal with typematic key repeats
        var keyCode:String = String(Key.getCode());
        if((undefined == keyState[keyCode]) || (0 == keyState[keyCode])){
                //this is a new press - set keyState for this key
                keyState[keyCode]=1;
                //YOUR CODE GOES HERE
                trace("Key pressed ");
        } else {
                // key was pressed before - this is a typematic event, so ignore it
                ;
        }
 }
 
 keyListener.onKeyUp = function () {
        //clear keyState for this key
        var keyCode:String = String(Key.getCode());
        keyState[keyCode]=0;
        //YOUR KEY RELEASE CODE GOES HERE
    trace ("You released a key.");
 }
 
 
 Key.addListener(keyListener);

 

Pressing multiple keys at the same time in Flash

An annoying detail: Apparently Flash has a bgug/feature that results in "lost" key up events when more than one key is pressed at a time. So if you push button1 and hold it, then button2 and hold it, releasing button 1 will NOT generate a key up event within Flash. This bug is documented (google for it) by others and some people have written workarounds.

Debouncing

When you depress an electromechanical switch, the electrical connection does not simply go from 0 to 1. The transition is often "noisy" and you get lots of rapid transitions between on and off within the first couple of milliseconds. The process of filtering out these spurious on/off events is called "switch debouncing". The Ipac has internal debouncing routines. For more on this topic, see e.g. http://www.ece.utep.edu/courses/web3376/concepts/debounce.html.

 

-Bjoern Hartmann (bjoern@cs) 2/14/2007