开发者

Fire event on Control+C, but still allow copy

开发者 https://www.devze.com 2023-03-17 01:44 出处:网络
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 actual

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 == ke开发者_运维百科y.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");
    }
});


Remove the line return false;

0

精彩评论

暂无评论...
验证码 换一张
取 消