Jake
Jake

Reputation: 4234

Fire event on Control+C, but still allow copy

I'm trying to run some code when the user presses control+c on a webpage, but I still want that key action to copy whatever the user has selected. With my current code, only the events I write actually happen and the default action is ignored.

Is this possible? Here's the code I'm using:

Code for control key functionality

$.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;
    });
};

Code for Control+C:

$('input').click(function(){
    $(this).addClass('selected');
    $(this).focus();
    $(this).select();
});

$('input').blur(function(){
    $('input.selected').removeClass('selected');
});

$.ctrl('C', function() {
    if( $('input.selected').length != 0 )
    {
        alert("Saved");
    }
});

Upvotes: 2

Views: 1117

Answers (1)

jerluc
jerluc

Reputation: 4316

Remove the line return false;

Upvotes: 2

Related Questions