5K Morse Code App Using Capslock LED

This is probably my first attempt at “literary programming”, though using that phrase may be taking liberties a little. In this case by “literary programming” I mean – programming inspired by literature. The literature in question is Cryptonomicon by Neal Stephenson. Cryptonomicon already features a Perl script in it’s pages, but that’s not what I’m talking about here. Instead there’s a chapter later in the book when Randy Waterhouse has been incarcerated. However he’s been given the use of his laptop so that – his captors hope – he will decrypt a key piece of data whilst they are monitoring his screen via Van Eck Phreaking. Randy cottons onto this, so he creates an app that will convert text to morse code and have that morse code emitted via the LEDs on his laptop’s keyboard.

I suddenly decided to have a go at creating such a morse code script for my Macbook and here’s the result in action:

You can test the script out by running this at the command line (from the directory with the script in):

echo 'sos' | python morse.py -led

It depends on libraries that are only found in OS X 10.5 Leopard for controlling the capslock LED.

With a bit of compression it all fits well under 5K of code, so makes a worthy example of a 5K app. The morse part of the app is very simple – apart from the lookup table, it could probably fit into 2-3 lines of code! The bulk of the code is simply concerned with driving the capslock key’s LED via the HID. For this I cribbed heavily from some of Apple’s sample code and converted it to run in Python using ctypes.

The only problem I found was that for some reason they internal keyboard on my Macbook stops responding to the request to change the LED state after several seconds. However using this script with an external USB keyboard worked fine. I guess the internal keyboard has some sort of abuse-prevention built-in.

7 thoughts on “5K Morse Code App Using Capslock LED

  1. Seg faults when run with -led argument on 10.6.7. Works fine with -led. As a note, the apple HID tool test example binary works fine on the same machine.

  2. Wouldn’t be surprised if it breaks on Snow Leopard – am only on 10.5.8 here. Was rather quickly hacked out, as something of a novelty. There’s probably a 32bit vs 64bit issue causing trouble somewhere…

  3. looking for a program that will take typing from my key board and convert direct to morse will this do it ???

  4. norman,

    this is only a demo/plaything. In principle it will take what you type and emit it as morse using the caps lock key LED on a macs keyboard. It might work for you, but it might not.


  5. i would love to see a simple python program that we can use to play with the led on lion. anyone know if this is possible? I want to use it as a mail notifier/other things based off applescripts.

  6. Sure it’s certainly possible, but pretty certain this code won’t work on Lion. It was a quick hack when I wrote it and that was on Leopard (10.5). Just a question of figuring out which libraries etc would work now to do it.

Comments are closed.