1.21 gigawatts
1.21 gigawatts

Reputation: 17746

Catch save (Ctrl/Cmd+S) keyboard shortcut in the browser?

Is there a cross-browser way or cross browser solution to capture the "Save" keyboard shortcut in the browser? For example, in GMail if you use CMD + S it will save the email and not prompt the browser to download the webpage (default behavior). I would rather use a cross browser solution than add event listeners and write it from scratch.

Upvotes: 1

Views: 496

Answers (1)

Sunny Patel
Sunny Patel

Reputation: 8078

One cross browser solution is to check out keycode.js. Less hassle to worry about looking for browser caveats and such.

I know it's old, but it still works. You can check out its demo too!

Looking at the demo, and the header comments in the .js, something like this?

document.onkeydown = function(e) {
    e = e || window.event
    var k = KeyCode.translate_event(e);
    if( k.ctrl && !k.alt && !k.shift && k.code == 83 ) //83 is the code for s
        save(); //Magic function
    if(e.preventDefault) {
        e.preventDefault();
    }
    return false;
};

Upvotes: 2

Related Questions