I'm working on an application where it should be possible to run a function if the user presses ENTER regardless of where the focus is on the page (eg. in a textbox or on a button etc).

Below is the code i've experimented with, just to find out how to react when the user presses ENTER, but it doesn't work. What am I doing wrong? Thanks in advance!

var Capsule = {

init: function() {
    var button = document.getElementById("txtInput");
    button.focus();

    Capsule.events();
},

events: function() {
    function checkKeyboard(e) {
        if (e.keyCode == 13) {
            console.log("Pressed ENTER.");
            return false;
        }
    }
}
}

window.onload = Capsule.init;

没有正确的解决方案

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top