views:

24

answers:

2

I am looking for a way to detect cmd+r and cmd+w (also ctrl+r and ctrl+w) in my application.

This site does it http://gpl.internetconnection.net/vi/. The source is a nightmare though. If I don't find a quick answer, I'll look through that.

Does anyone have a link or example?

+2  A: 

using jQuery:

$.ctrl = function(key, callback, args) {
    var isCtrl = false;
    $(document).keydown(function(e) {
        if(!args) args=[]; // IE barks when args is null

        if(e.ctrlKey) isCtrl = true;
        if(e.keyCode == key.charCodeAt(0) && isCtrl) {
            callback.apply(this, args);
            return false;
        }
    }).keyup(function(e) {
        if(e.ctrlKey) isCtrl = false;
    });
};

This taken from: http://www.gmarwaha.com/blog/2009/06/16/ctrl-key-combination-simple-jquery-plugin/

Benny
I would suggest, as Benny shows, to use a framework, such as jQuery, for detecting such input, as generally it's browser dependent and can be a heck to make it cross browser compatible doing it manually
azatoth
Yeah, I agree. I also found this: http://plugins.jquery.com/project/hotkeys. I'm using Ext JS, so I may just use that framework as described here: http://www.sencha.com/forum/archive/index.php/t-5759.html
Chad Johnson
A: 

I ended up doing what's described here: http://www.sencha.com/forum/archive/index.php/t-5759.html

Works perfectly.

Chad Johnson