I would like to detect keyboard events without having to click into the window first. Keyboard events should not have to rely on a mouse so this seems strange. I am using the latest version of Firefox.
I have tried first adding focus to the window, document and body in a few ways but so far no luck.
I would give a jsfiddle link but that runs inside an iframe which I think is a different situation and solution.
Here is what I have tried:
(No jQuery please!)
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title></title>
<meta name="description" content="">
<meta name="author" content="">
</head>
<body>
<p>Hello world.</p>
<script>
document.addEventListener("DOMContentLoaded", function(e) {
// none of these seem to help
document.body.focus();
//document.focus();
//window.focus();
//top.document.focus();
//parent.document.focus();
//document.documentElement.focus();
});
// not triggered until after clicking in the window
document.addEventListener("keydown", function(e) {
console.log("keydown", e);
});
</script>
</body>
</html>
Update
With the developer tools closed, the keyboard events trigger fine (without the need to set focus).
How can we take the focus back away from the console or developer tools window (even though it is unlikely that a regular user would have them open)?