1

Is there a way to check if Shift + letter is pressed in lodash's debounce?

fetchDebounced: _.debounce(function(e) {
  if (e.keyCode === 16 && e.keyCode >= 46 && e.keyCode <= 90) {
    console.log('shift + letter is pressed')
  }
}

Doesn't work in my case, using it on keyup event. I want to print out console message, only when SHIFT+LETTER, for example, Shift+a is pressed (or Shift+z). Is it possible?

Alexander Kim
  • 17,304
  • 23
  • 100
  • 157
  • If it *always* "doesn't work", its an easy fix. If not, can you explain when it "doesn't work"? – Scott Hunter May 07 '19 at 19:27
  • what listener you used, i forgot it, but there is specific way to add listeners to see if both buttons are pressed at same time. – Mladen Skrbic May 07 '19 at 19:27
  • 5
    This is a duplicate. See https://stackoverflow.com/questions/7479307/how-can-i-detect-shift-key-down-in-javascript – DaveCat May 07 '19 at 19:28
  • Your condition fails because the `keyCode` can't be between 46-90 at the same time. Try using the `shiftKey` property instead of checking the shift keyCode – Sterling Archer May 07 '19 at 19:28
  • https://stackoverflow.com/questions/5203407/how-to-detect-if-multiple-keys-are-pressed-at-once-using-javascript – Mladen Skrbic May 07 '19 at 19:29
  • `e` (event object) has `.shiftKey` property (among many others). – Alex Kudryashev May 07 '19 at 19:29
  • 4
    Possible duplicate of [How can I detect shift + key down in javascript?](https://stackoverflow.com/questions/7479307/how-can-i-detect-shift-key-down-in-javascript) – isherwood May 07 '19 at 19:39

4 Answers4

3

You can use onkeydown and e.shiftKey:

document.onkeydown = function(e) {
  if (e.shiftKey && e.keyCode >= 46 && e.keyCode <= 90) {
    console.log("SHIFT + " + e.keyCode);
    return false;
  }
}

Alexander Kim
  • 17,304
  • 23
  • 100
  • 157
Timofey Goncharov
  • 1,023
  • 1
  • 6
  • 14
0

As per this answer:

event.shiftKey is a boolean. true if the Shift key is being pressed, false if not. altKey and ctrlKey work the same way.

So basically you just need to detect the keydown as normal with onkeydown, and check those properties as needed.

Community
  • 1
  • 1
Das_Geek
  • 2,775
  • 7
  • 20
  • 26
0

To achieve expected result, use below option of using shiftkey press and keyCode check between 65-90(A-Z) on key up to track as one event

function checkCode(e) {
    if(e.shiftKey){
    console.log(e.shiftKey && (e.keyCode >= 65 || e.keyCode <= 90))      
    }
}
<input onkeyup = "checkCode(event)" />

codepen - https://codepen.io/nagasai/pen/NVqMOg

Naga Sai A
  • 10,771
  • 1
  • 21
  • 40
0

Because people are answering, this is the best answer from this previously-asked question.

...onkeyup = function(e) {
    if (e.keyCode == 13)
    {
//      if (e.shiftKey === true)
        if (e.shiftKey)  // thruthy
        {
            // new line
        }
        else
        {
            // run your function
        }
        return false;
    }
}

Also note Das_Geek's answer above, that altKey and ctrlKey work the same way, per the venerable Niet the Dark Absol.

cssyphus
  • 37,875
  • 18
  • 96
  • 111