On any modern browser, you can use a keydown
or keyup
event handler like this one:
document.addEventListener("keydown", function(e) {
if (e.key === "F8" && e.shiftKey) {
e.preventDefault();
// ...Shift+F8 was pressed...
}
});
or with jQuery:
$(document).on("keydown", function(e) {
if (e.key === "F8" && e.shiftKey) {
e.preventDefault();
// ...Shift+F8 was pressed...
}
});
Whether that works may be browser-specific, because different browsers reserve different key combinations. It works on Chrome and Firefox on my copy of Linux, and on Edge on Windows 10, and IE11 on Windows 8. Note that you have to click inside the window to ensure it has focus.
You've tagged internet-explorer-8. If you really need to support that truly obsolete browser, which doesn't have addEventListener
, use a version of jQuery that still supports IE8 (or the hookEvent
function from this answer, which works on IE8 [or even IE6]) and then instead of using e.key
, use e.which || e.keyCode
and the appropriate keycode. The reason we have e.key
now is that "the appropriate keycode" varies by keyboard layout. On my keyboard, F8
is 119
, so:
// For obsolete browsers without `addEventListener`
hookEvent(document, "keydown", function(e) {
if ((e.which || e.keyCode) === 119 && e.shiftKey) {
e.preventDefault();
// ...Shift+F8 was pressed...
}
});
or using an older version of jQuery that supports IE8:
// For obsolete browsers
$(document).on("keydown", function(e) {
// ^---- if it's a REALLY old version, you might need `bind` instead of `on`
if ((e.which || e.keyCode) === 119 && e.shiftKey) {
e.preventDefault();
// ...Shift+F8 was pressed...
}
});