Week 2: Basic Electronics, and a Simple Replacement for the Keyboard

basic electronics concepts : voltage, current, resistance. circuits and switches. how to take apart a keyboard; mapping the grid. taking apart mice.

Keyboard input in Processing

There are two methods for getting keyboard input in Processing - by testing a variable, or by using a function. the keyPressed variable will tell you if there is currently a key being pressed. check it in the draw() loop every frame - if it's true, then you can use the key and keyCode variables to find out which key it is.
// from the processing reference

void draw() {
  if (keyPressed == true) {
    fill(0);
  } else {
    fill(255);
  }
  rect(25, 25, 50, 50);
}
while the keyPressed() function lets you define code to be called when a key is pressed. This way, you don't have to keep checking the state of the keyboard yourself - Processing does that for you. Note that keyPressed() here has parentheses after it - that's how you can tell the difference between a function and a variable.
 	
// Click on the image to give it focus,
// and then press any key

int value = 0;

void draw() {
  fill(value);
  rect(25, 25, 50, 50);
}

void keyPressed() 
{
  if(value == 0) {
    value = 255;
  } else {
    value = 0;
  }
}

There's also a keyReleased() function that goes with keyPressed(). You can use either method in general, but it may be better practice to use the first method - checking the state of the keyPressed variable - because it is similar to the way you check sensor values using a serial interface board.

Keyboard hack: Resources

You'll need a spare keyboard, keypad, or mouse. Keypads are sometimes cheaper than full keyboards, but generally both go for around $20. If you are using a PC or an older Mac with ADB support, you can also pick up used keyboards for as little as $3 at thrift stores. You can also get adapters to use PS/2 or ADB keyboards in a USB port; these are usually about $30, so only make sense if you're planning to build more than one of these types of interfaces.

Hacking it

a keyboard is basically an object with 100-some odd buttons on it. by replacing any of these with other kinds of digital switch-type inputs, you can turn the keyboard into an interface for a wide range of sensors and devices. for example, you might replace the buttons with floor pad switches, to create a new very large keyboard. The benefit of the keyboard hack versus interface boards like the EZIO, Arduino, Teleo, etc. is that you don't have to do any work with drivers, serial ports, communication protocols, special code, etc - whatever interface you build appears to the computer to be just a regular keyboard. This means you can use with the keyboard functions in Processing, or in Director, or MAX/MSP, or Flash, or an arcade game emulator, or a text-based web browser. You could even just use Word, record a bunch of macros and map them to different keys, and then connect the keys to a bunch of motion detectors around the room.

once you open the keyboard, you'll find that it's not quite that easy - most modern keyboards are made with membrance switches rather than physical buttons, which means you can't just desolder something and replace it with something else. instead, we interface straight to the controller chip in the keyboard, which responds to connections between combinations of pins in a matrix of inputs. the major work of this hack involves mapping out which combos of pins control which keys. This is a hack which has been around for years, and is particularly popular in the MAME (arcade game emulation) scene. here are some links to different instructions and resources; most are oriented towards using this as part of a home-made arcade game cabinet, which is pretty exciting anyway, but of course the beauty of this hack is that a hacked keyboard is usable with ANY program.

Alternatively

Once the work of mapping out the keyboard matrix gets old, another solution is to buy a standalone keyboard encoder. This is basically like the chip you're hacking into, but already nicely packaged with documentation. It's a little more expensive, but easier.