0
if(event.keyCode === 13 ){
    return false;
  }

basically this simple code above disables a specific key, now i wanted to disable a lot of keys so i came up with something like

if(event.keyCode === 13 || event.keyCode === 14 || event.keyCode === 15 || event.keyCode === 16 , etc)

is there a faster way to do this instead of having to put a lot of ||s?

im not really sure if this has been asked before but i tried searching and couldnt find anything

I_love_vegetables
  • 1,644
  • 5
  • 11
  • 25

2 Answers2

1

You can store the keyCodes in a array and check against the array with includes()

const keyCodes = [13, 14, 15, 16];

if (keyCodes.includes(event.keyCode)) {
  //do something
}
ikhvjs
  • 4,417
  • 2
  • 6
  • 23
1

Make an array of keycode you want to disable:

const disabledKeys = [13, 14, 15, 16];

const isKeyDisabled = (keyCode) => disabledKeys.indexOf(event.keyCode) > -1

if(isKeyDisabled(event.keyCode)){
  return false;
}
Baboo
  • 3,326
  • 2
  • 15
  • 26