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?

7
  • If it always "doesn't work", its an easy fix. If not, can you explain when it "doesn't work"? Commented May 7, 2019 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. Commented May 7, 2019 at 19:27
  • 5
    This is a duplicate. See stackoverflow.com/questions/7479307/… Commented May 7, 2019 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 Commented May 7, 2019 at 19:28
  • 4
    Possible duplicate of How can I detect shift + key down in javascript? Commented May 7, 2019 at 19:39

4 Answers 4

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; } } 
Sign up to request clarification or add additional context in comments.

1 Comment

My issues was with using keyup instead of keydown that's why my shiftKey was never true.
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.

1 Comment

what is event in this example? console.log(event) outputs undefined for me
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

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.