Tag Archives: Javascript

capturing + (plus key) with keymaster.js

We use keymaster.js to power the keyboard shortcuts on a new product under development at work. It’s a great little javascript library for detecting key-presses from users.

For this product we wanted to capture the “+” key for when users wanted to Add something to their console. The confusion for me came from the fact the the “+” is used in the key() method for joining sequences of keys to get multi-key dispatching (e.g. pressing Control and Return at the same time).

It’s a pretty simple solution, but I couldn’t find it anywhere so thought I’d post here just in case someone else wants to do the same thing. As it turns out, “=” and “+” share the same key code in javascript, so the following will allow capturing of the plus key from both the numeric keypad as well as on the standard keyboard.

<script type="text/javascript" src="keymaster.min.js"></script>
<script type="text/javascript">
(function() {
  var myFunc = function() {
    console.log("User hit the + key");
  };
  key('=,shift+=', myFunc);
}());
</script>

An unfortunate by-product of the above, however, is that the same function will fire if the user hits just the “=” key. A pretty trivial trade-off IMHO. If anyone can think of a way to differentiate between the two let me know!

Extending Prototype’s Template class to allow default values

I’ve often wanted to extend Prototype’s Template class to allow setting default values for template fields if the user doesn’t pass them, so I wrote a simple wrapper class that does just that.

Continue reading

Hosting ga.js locally (google analytics tracking code)

Hosting ga.js locally has two main benefits – fewer DNS lookups for the client’s browser (you could even combine it with your site’s java script to reduce HTTP reqs as well), and you might be able to get it to your users faster than Google can. Whilst the second point isn’t likely true for many, it is true for those of us serving most of our content in Australia.

Google recommends you don’t do this, but doesn’t say you can’t.
Continue reading