June 25, 2014

Using Keypress with RequireJS

Replacing the Raspberry PI with an Arduino Yún in the Athlete 1 Rover has breathed new life into the project. After porting the code to the Yún, I thought it’d be cool to allow users to drive the rover using keyboard arrows and sets of keys like WASD. I thought about writing the required JavaScript from scratch, but, after thinking more about what I needed and taking a look at available libraries, I decided to give the Keypress JavaScript library a shot.

I’m not going to go into the reasons why I choose Keypress right now. Suffice it to say, given its original focus on input for games, it makes a great choice for control applications in general.

The author was kind enough to provide AMD support. However, the documentation isn’t completely clear about using Keypress as an AMD module. The documentation states that a listener can be instantiated like so:

var listener = new window.keypress.Listener();

However, if you’re using Keypress and an AMD module, the object is made available as ‘keypress’ as opposed to ‘window.keypress’. So, in this case, the listener should be exposed like so:

var listener = new keypress.Listener();

Hopefully, this will save somene out there some time.

Posted in: JavaScript