Home >Web Front-end >JS Tutorial >When Simulating Keydown in Chrome, Why Does the Wrong Key Get Registered?

When Simulating Keydown in Chrome, Why Does the Wrong Key Get Registered?

Susan Sarandon
Susan SarandonOriginal
2024-10-18 13:16:30968browse

When Simulating Keydown in Chrome, Why Does the Wrong Key Get Registered?

Keydown Simulation in Chrome Fires Normally but Not the Correct Key

Simulating keydown events on a textarea element in Chrome is essential for tailored user interactions. However, certain attempts may yield unexpected results.

You attempted to initiate a keydown event using initKeyboardEvent with the desired keyCode, but the textarea ended up registering an incorrect key value (Enter instead of the intended letter). An alternate approach involving property overriding also proved unsuccessful.

To address this issue, you need to override the 'which' property in addition to 'keyCode'. Here's a modified code sample:

<code class="js">document.addEventListener('keydown', e => console.log(
  'altKey                : ' + e.altKey + '\n' +
  'charCode (Deprecated) : ' + e.charCode + '\n' +
  'code                  : ' + e.code + '\n' +
  'ctrlKey               : ' + e.ctrlKey + '\n' +
  'isComposing           : ' + e.isComposing + '\n' +
  'key                   : ' + e.key + '\n' +
  'keyCode (Deprecated)  : ' + e.keyCode + '\n' +
  'location              : ' + e.location + '\n' +
  'metaKey               : ' + e.metaKey + '\n' +
  'repeat                : ' + e.repeat + '\n' +
  'shiftKey              : ' + e.shiftKey + '\n' +
  'which (Deprecated)    : ' + e.which + '\n' +
  'isTrusted             : ' + e.isTrusted + '\n' +
  'type                  : ' + e.type
));

const Podium = {};
Podium.keydown = function(k) {
  const oEvent = document.createEvent('KeyboardEvent');

  // Chromium Hack
  Object.defineProperty(
    oEvent,
    'keyCode',
    {
       get : function() {
         return this.keyCodeVal;
       }
    }
  );
  Object.defineProperty(
    oEvent,
    'which',
    {
       get : function() {
         return this.keyCodeVal;
       }
    }
  );

  if (oEvent.initKeyboardEvent) {
    oEvent.initKeyboardEvent("keydown", true, true, document.defaultView,
                             false, false, false, false, k, k);
  }
  else {
      oEvent.initKeyEvent("keydown", true, true, document.defaultView,
                          false, false, false, false, k, 0);
  }

  oEvent.keyCodeVal = k;

  if (oEvent.keyCode !== k) {
    alert("keyCode mismatch " + oEvent.keyCode + "(" + oEvent.which + ")");
  }

  document.dispatchEvent(oEvent);
}

//Sample usage
Podium.keydown(65);</code>

By overriding both 'keyCode' and 'which', you ensure that the correct key code and corresponding key are registered during keydown events. This approach rectifies the incorrect key registration issue and allows for accurate keydown simulations.

The above is the detailed content of When Simulating Keydown in Chrome, Why Does the Wrong Key Get Registered?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn