简体   繁体   中英

How do I get key values in HTML/Javascript

Okay, so i understand how to get the key value while using an input field... but I am taking about key values that are pressed while your browser isn't focused in any text box or text area.

I am trying to make a onscreen keypad that has buttons for 0, 1, 2, .. 9... however I want the user to be able to press the buttons with the keys on the keyboard.

I've seen this done in some websites, where if you press the S key on the homepage, it will take you to the signin screen. Facebook also does the L key, to like a photo.

So the question is: How do I get the key values in javascript, when the cursor isn't focused.

If you are using JQuery you just add the event handler to the document...

$(document).keypress(function(event) {
      alert('Handler for .keypress() called. - ' + event.which);
});

(From http://forum.jquery.com/topic/how-to-catch-keypress-on-body )

Edit for zzzzBov's comment...

From the JQuery KeyPress documentation:

To determine which character was entered, examine the event object that is passed to the handler function. While browsers use differing properties to store this information, jQuery normalizes the .which property so you can reliably use it to retrieve the character code.

you need to use window.onkeydown and then check for the keys you're interested in.

https://developer.mozilla.org/en-US/docs/Web/API/window.onkeydown

You should listen on key press event.

document.onkeypress = function(evt) {
    evt = evt || window.event;
    var charCode = evt.which || evt.keyCode;
    alert("Character typed: " + String.fromCharCode(charCode));
};

For more info Look here Link

You need to add an event listener to the window. Then in the event handler, you get the keyCode property from the passed-in event. KeyCodes are semi-arbitrary in that they don't directly map to what you might think, so you have to use a table (first result on google: http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes ) to identify the keycodes you care about.

window.addEventListener('keypress',function (evt) {
   switch (evt.keyCode) {
       case 48:
           zeroKeyPressed(); break;
       case 49:
           oneKeyPressed(); break;
       ...
   }
}, false);

You would use a key press event.

Here's an example for your usage:

window.addEventListener('keypress', function (event) {

    var key_code, key;

    event = event || window.event; // IE

    key_code = event.charCode || event.keyCode || event.which || 0;
    key = String.fromCharCode(key_code);

    // prevent keys 0-9 from doing what they normally would do
    if (key_code >= 48 && <= 57) {
        event.preventDefault();
        alert('The user pressed ' + key);
    }

}, false);

Using plain js, you can use this in your layout.htmlcs, at the beginning:

  @{

        <script>
            sessionStorage.setItem("ProductionHostURL", '@System.Configuration.ConfigurationManager.AppSettings["ProductionHostURL"]');
        </script>

}
<!DOCTYPE html>

Then in your main js file of the layout.htmlcs, you can use this a method liked this:

var urlBaseProduction;
var urlBaseDevelopment;

    $(document).ready(function () {
      configureHostEnvironment()
     ....
    }

In that method, configure the variables to use in production and development, like this:

function configureHostEnvironment(){

    HOST = sessionStorage.getItem("ProductionHostURL")
    if (HOST.length <= 0) {
        alert("Host not configured correctly")
    } else {
        urlBaseProduction= host + '/api/';
        urlBaseDevelopment= host + port + '/api/';
    }
}

If you have a suggestion or improvement to this method, please comment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM