Search code examples
javascriptjqueryhandsontable

Keyboard shortcut not working


I am using HandsOnTable jquery plugin, and I'm trying to handle a specific key combination (Alt+v) shortcut. But it is not working for some reason, here is my code and jsfiddle:

$(document).ready(function () {
var isCtrl = false;
var isShift = false;
var isAlt = false;
// action on key up
$(document).keyup(function (e) {
    if (e.which == 17) {
        isCtrl = false;
    }
    if (e.which == 16) {
        isShift = false;
    }
    if (e.which == 18) {
        isAlt = false;
    }
});
// action on key down
$(document).keydown(function (e) {
    if (e.which == 17) {
        isCtrl = true;
    }
    if (e.which == 16) {
        isShift = true;
    }
    if (e.which == 18) {
        isAlt = true;
    }
    if (e.which == 86 && isAlt) //alt+v
    {
        console.log("alt+v detected");
        e.preventDefault();
        e.stopImmediatePropagation();
        return false;
    }
});
});

Solution

  • I'm using chromium and it looks like all the keydown events do not fire. I found out there is a beforeKeyDown callback, which can be used to "modify keybindings". Using that seems to work:

    $('#example').handsontable({
        data: data,
        minSpareRows: 1,
        colHeaders: true,
        contextMenu: true,
        beforeKeyDown: function (e) {
            if (e.altKey === true && e.which === 86) {
                console.log("alt-v");
                e.stopImmediatePropagation();
                e.preventDefault();
            }
        }
    });
    

    http://jsfiddle.net/JdzR3/