Userscript not capturing ctrlKey keydown event

102 views Asked by At

The following userscript is not capturing the ctrlKey event.

// ==UserScript==
// @name      CtrlKey alert
// @namespace CtrlKey alert
// @include   *
// @run-at    document-start
// ==/UserScript==

document.addEventListener(
  "keydown",
  function (zEvent) {
    if (zEvent.ctrlKey) {
      zEvent.preventDefault();
      alert("CtrlKey pressed!");
    }
  },
  true
);

An identical user script can capture any other key event, though:

// ==UserScript==
// @name      Key A alert
// @namespace Key A alert
// @include   *
// @run-at      document-start
// ==/UserScript==

document.addEventListener(
  "keydown",
  function (zEvent) {
    if (zEvent.key === "a") {
      zEvent.preventDefault();
      alert("Key A pressed!");
    }
  },
  true
);

I have tried using zEvent.stopPropagation, zEvent.stopImmediatePropagation, and zEvent.preventDefault individually but they have had no effect (this is important because the ultimate goal is to override a default ctrl+letter command like ctrl+f).

I have tried substituting document.addEventListener('keydown', function(zEvent) with document.onKeydown = function(zEvent) with no results.

Everything I've read indicates that keydown or onKeydown should capture ctrlKey, yet it doesn't. Is there something specific about ctrlKey I'm missing that's preventing javascript from capturing it?

Running Firefox v105.0.1 on Windows 11

0

There are 0 answers