capricorn86 / happy-dom

A JavaScript implementation of a web browser without its graphical user interface
MIT License
3.08k stars 185 forks source link

KeyboardEvent is missing getModifierState #1467

Open ImLunaHey opened 2 weeks ago

ImLunaHey commented 2 weeks ago

Describe the bug getModifierState is undefined on KeyboardEvent.

To Reproduce

const event = new KeyboardEvent('keydown', { key: ']' });
console.info(event.getModifierState('Shift') === false);

this should return true instead i get TypeError: event.getModifierState is not a function

https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/getModifierState

ImLunaHey commented 2 weeks ago

maybe something like this?

KeyboardEvent.prototype.getModifierState = function (key: string) {
  const modifierKeys = {
    Alt: this.altKey,
    Control: this.ctrlKey,
    Meta: this.metaKey,
    Shift: this.shiftKey,
  };

  return !!modifierKeys[key];
};