简体   繁体   English

keyup 事件未设置事件 shiftKey JAVASCRIPT

[英]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. function onkeyup 在同时按下 SHIFT 键和字符,或者按住 SHIFT 键并按下字符时,所有字符都可以正常工作,但是当在字符之前按下 SHIFT 键时会出现我的问题。 The value returned is with lowercase characters.返回的值是小写字符。 So for example if I key up SHIFT and press a, 'a' is returned but not 'A'.因此,例如,如果我按下 SHIFT 并按 a,则返回“a”但不返回“A”。

So my question is how do to SHIFT key is being keyup.所以我的问题是如何对 SHIFT 键进行键控。 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.我假设您想按下并释放“Shift”按钮并单击任何其他字符。 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" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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