views:

2479

answers:

8

My users would like to be able to hit Control+S to save a form. Is there a good cross-browser way of capturing the Ctrl+S key combination and submit my form?

App is built on Drupal, so jQuery is available.

+8  A: 
$(window).keypress(function(event) {
    if (!(event.which == 115 && event.ctrlKey)) return true;
    alert("Ctrl-S pressed");
    event.preventDefault();
    return false;
});

Key codes can differ between browsers, so you may need to check for more than just 115.

Jim
on OS X webkit browsers cmd+s returns 19, so it is also worth checking for that. i.e. if (!(event.which == 115
Tadas Tamosauskas
+8  A: 

You could use a shortcut library to handle the browser specific stuff.

http://www.openjs.com/scripts/events/keyboard_shortcuts/#

shortcut.add("Ctrl+S",function() {
    alert("Hi there!");
});
EndangeredMassa
+4  A: 

I would like Web applications to not override my default shortcut keys, honestly. Ctrl-S already does something in browsers. Having that change abruptly depending on the site I'm viewing is disruptive and frustrating, not to mention often buggy -- I've had sites hijack Ctrl-Tab because it looked the same as Ctrl-I, both ruining my work on the site and preventing me from switching tabs as usual.

If you want shortcut keys, use the accesskey attribute. Please don't break existing browser functionality.

Eevee
I agree, entirely, but unfortunately I'm the peon implementing, not the decision maker.
ceejayoz
True, but CTRL+S is not an often-used feature. I doubt people would miss it, especially if it means you can save your file in your webapp.
EndangeredMassa
A: 

@Eevee: As the browser becomes the home for richer and richer functionality and starts to replace desktop apps, it's just not going to be an option to forgo the use of keyboard shortcuts. Gmail's rich and intuitive set of keyboard commands was instrumental in my willingness to abandon Outlook. The keyboard shortcuts in Todoist, Google Reader, and Google Calendar all make my life much, much easier on a daily basis.

Developers should definitely be careful not to override keystrokes that already have a meaning in the browser. For example, the WMD textbox I'm typing into inexplicably interprets Ctrl-Del as "Blockquote" rather than "delete word forward". I'm curious if there's a standard list somewhere of "browser-safe" shortcuts that site developers can use and that browsers will commit to staying away from in future versions.

Herb Caudill
The answer is No, there is no safe list of browser-safe shortcuts. And it's good for two resaons:1. Shortcut are customizable (at least in Opera).2. Doing so, you don't restrict browser vendor creativity to give you more power for the browser usage itself.
gizmo
A: 

Cool. thanks for this shortcut key... loved it.

+1  A: 

Problem with the shortcut method is that it may interfere with the functioning of screen readers.

That's up to the screenreader. If keyboard shortcuts are vital to a screenreader's operation, it shouldn't allow them to be overridden.
ceejayoz
A: 

I like this little plugin here .. needs a bit more cross browser friendliness though:

http://code.google.com/p/js-hotkeys/

misteraidan
A: 

example:

shortcut.add("Ctrl+c",function() { alert('Ok...');},{ 'type':'keydown', 'propagate':false, 'target':document });

usage

link for download: http://www.openjs.com/scripts/events/keyboard_shortcuts/#

André