PeterF
PeterF

Reputation: 61

How to determine when an accesskey is pressed

How to determine when an accesskey is pressed - with javascript(IE, Chrome, Opera, Safari). In FF I used document.onkeypress event, but in Chrome this event doesn't fire when ALT key is pressed.

Thanks in advance :).

Upvotes: 1

Views: 514

Answers (1)

photo_tom
photo_tom

Reputation: 7342

Take a look at how it is done in http://plugins.jquery.com/project/KeyTips. This is an excellent library for visually displaying what accessKeys are assigned to what HTML elements.

The key section of code is --

$(document)
.bind("keydown.keytips", function (e) {
    if (!accessKeysHighlighted && (
            (e.keyCode == 18 && !requiresShiftAlt) ||
            (e.keyCode == 16 && e.altKey && requiresShiftAlt) ||
            (e.keyCode == 18 && e.shiftKey && requiresShiftAlt))) {
        // Highlight all the access keys
        highlightAccessKeys();
        //accessKeysHighlighted = true;
    }
})
.bind("keyup.keytips", function (e) {
    // Un-highlight access keys
    if (accessKeysHighlighted) {
        unhighlightAccessKeys();
        //accessKeysHighlighted = false;
    }
});

Upvotes: 0

Related Questions