Archive for February 22nd, 2013

This post is obsolete; the $.keymap has changed significantly. Documentation is now at http://bililite.com/blog/2015/01/12/rethinking-keymap/.

I've been playing around with creating an online editor, and one thing it needs is keyboard shortcuts. Control keys and the like don't register keypress events, so I have to use keydown to get the event.which (and I have to use jQuery to normalize that across browsers), and then I have a random keyboard code and a bunch of modifier key flags to work with. The W3C once tried to rationalize all this, but no one picked up on it. So I wrote a jQuery plugin to turn the event into a friendlier string (using a notation based on Microsoft's sendkeys). Thus:

  • Pressing the 'a' key gives $.keymap(event) == 'a'
  • Pressing the 'a' key with shift held gives $.keymap(event) == 'A'
  • Pressing the 'a' key with control held gives $.keymap(event) == '^A'
  • Pressing the 'a' key with alt held gives $.keymap(event) == '%A'
  • Pressing the 'a' key with control and alt held gives $.keymap(event) == '^%A'
  • Pressing the 'Esc' key gives $.keymap(event) == '{esc}'
  • Pressing the 'Esc' key with shift held gives $.keymap(event) == '+{esc}'
  • Pressing the '*' key on the numeric keypad gives $.keymap(event) == '{multiply}'
  • Pressing the '1' key on the numeric keypad with control held gives $.keymap(event) == '^{1}'

Download the code.

See the demo.

Continue reading ‘Parsing keydown events’ »