Thursday 3 March 2011

Keyboard I/O hack

During my research visit at Microsoft Research in Cambridge with Steve Hodges group I spend a lot of time building things and programming (Windows Phone 7, C#, microcontroller...) - which I really enjoyed!

In this post I want to share a mini project, I did together with Nic Villar between two coffees … it reminded me somehow of the Friday afternoon projects (=2h projects you do because they are fun and sometimes you learn something) while I did my PhD.

Perhaps some context first: working on Microsoft .NET Gadegteer we discussed many ideas of how to ease code/system creation for physical and embedded computing. Looking into it I came across a number of interesting approaches for software development that runs in the browser. Examples include: http://ideone.com/ (a online compiler for a large number of languages), http://wonderfl.net/ is a online development environment for Flash that allows to build on other people's code by forking their project, and http://mbed.org/ is a online tool for rapid prototyping of a microcontroller system.

Now to the mini project: a keyboard hack that supports input as well as output. You find many example of keyboard hacks on the web - basically you take out the PCB of the keyboard an replace the keys with something more interesting… here is ours that supports input (which is common) as well as output - from the computer (or web browser) to the environment. Using AQW210EH allows more flexibility in what you drive and what you create the inputs with. The attractive thing in using a keyboard is that it will work without a driver - you just plug it in and it works.

To control it I wrote a java applet and some JavaScript code. Basically the java-applet is controlling the Scroll-Lock LED and from the website this is called via JavaScript (try here if it works on your computer - if it works your CAPS, NUM, and SCROLL LED will blink once the web page is open). I tried it on some Windows machines and it worked well - the java applet seems not to work on a Mac (so you probably have to find another way to set you Scroll Lock LED programmatically). You can extend the output to CAPSLOCK and NUMLOCK - but these are sometimes used - in contrast to the SCROLL LOCK. Input is simple - by connecting one of the rows to one of the colloms you generate a letter comes in as if you would type it. The nice thing with USB is that you can have multiple keyboards connected at the same time (however the LEDs are synchronized between them).

In C# you can use the SendKeys.Send("{SCROLLLOCK}") command (for details see: http://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.send.aspx,
http://support.microsoft.com/Default.aspx?id=177674) The SendKeys.Send may only give an pulse to the LED and hence you may need to call a system function.

Using a JavaApplet it looks like this.

import java.applet.*;
import java.awt.*;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
public class keyLed extends Applet {
Font f = new Font("TimesRoman",Font.BOLD,20);
String strMessage;
Toolkit tk = Toolkit.getDefaultToolkit();
public void init() {
setBackground(Color.white);
setStrMessage("Applet loaded.");
}
public void paint(Graphics g) {
g.setFont(f);
g.drawString(this.strMessage, 100 , 25);
}
public void ScrollLedOn() {
tk.setLockingKeyState(KeyEvent.VK_SCROLL_LOCK,true);
setStrMessage("Scroll on");
}
public void ScrollLedOff() {
tk.setLockingKeyState(KeyEvent.VK_SCROLL_LOCK,false);
setStrMessage("Scroll off");
}
}



And in HTML/JavaScript you call it:
<APPLET CODEBASE="." CODE="keyLed.class" WIDTH=300 HEIGHT=50 NAME="keyLedApplet">
</APPLET>
<script language="JavaScript" type="text/javascript"> 
function LEDOn()
{
document.keyLedApplet.ScrollLedOn();
}
 
function LEDOff()
{
document.keyLedApplet.ScrollLedOff();
}
 </script>