简体   繁体   中英

String.fromCharCode not return all values

I'm trying to alert any key-pressed :

alert(String.fromCharCode(e.which));

Seems to work for AZ az characters but not for any other characters.

My goal is to make it work for any character (WYSIWYG).

You can manually provide your own mappings, see my jsfiddle example: https://jsfiddle.net/Glogo/dab3hzz7/1/

sample code:

function myStringFromCharCode(which) {
    var strKey = String.fromCharCode(which);

    switch(which) {
        case 13: return "Shift"
        case 17: return "Ctrl"
        case 18: return "Alt"
        // add more mappings here ...
    }

    return strKey;    
}

function myOnKeyUp(e) {
    alert(e.which + "=" + myStringFromCharCode(e.which));
}

document.body.onkeyup=myOnKeyUp;

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