简体   繁体   中英

keyup event does not set event shiftKey JAVASCRIPT

The function onkeyup works fine with all the characters when both SHIFT key and character are pressed, or keeping SHIFT key pressed and key up the character, but my problem happens when key up the SHIFT key before the character. The value returned is with lowercase characters. So for example if I key up SHIFT and press a, 'a' is returned but not 'A'.

So my question is how do to SHIFT key is being keyup. I've tried the following check but this didn't work:

pass_input_obj[i]['input'].onkeyup = function(event) {`enter code here`
    if(event.key == "Process") {
        if(event.code.includes("Shift")) keypressed= "Shift";
     } else
          keypressed= event.key; 


if(keypressed == "Shift" || (event.code && event.code.includes("Shift"))) shiftclicked = false;

if(!isSpecialKey(keypressed) && !crtlclicked){
    Capletter = keypressed;

if(shiftclicked == true){
    Capletter = keypressed.toUpperCase();
}
}

Not sure if I got clear all your needs. I assume you want to press and release 'Shift' button and click any other character. That character then should be capitalized.

Here is my way of implementing it

 var inputEl = document.getElementById('txt'); var isPrevShiftClicked = false; inputEl.onkeyup = function(event) { if(isPrevShiftClicked) { inputEl.value = inputEl.value.slice(0, inputEl.value.length - 1) + inputEl.value.charAt([inputEl.value.length - 1]).toUpperCase(); } isPrevShiftClicked = event.keyCode === 16; }
 <input type="text" id="txt" />

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