Tag Archives: prototypejs

Hooking up Ctrl+S using JS+Prototypejs

Did you ever find yourself in a position where having Ctrl+S hooked up as an event would be great? Well I have and doing a fast search on google gave me this page OpenJs.com – Keyboard shortcuts.
While this page is great, I needed to have a script that was written in prototypejs, since this is what we use at work (It’s a left over from the early Rails days, where prototypejs was tha bomb).

Since this really isn’t a big thing to do in prototypejs and if you use firebug you could inspect the event object in the dom yourself, I’ll just give you the script:

$(document).observe('keypress', function(e) {
  if(e.charCode==115 && e.ctrlKey) {
    e.stop();
    console.log('CTRL+s was called!');
  }
});

The only important thing to remember, is to call e.stop(), as this prevents the browsers “Save Page” functionality from opening.