• Login
    • Search
    • Categories
    • Recent
    • Tags
    • Users
    • Groups
    • Rules
    • Help

    Do more on the web, with a fast and secure browser!

    Download Opera browser with:

    • built-in ad blocker
    • battery saver
    • free VPN
    Download Opera

    How send CTRL+I from Tampermonkey script.

    Opera for Windows
    2
    3
    439
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • MajesticF
      MajesticF last edited by

      How send CTRL+I from Tampermonkey script to browser.

      I try to send in Opera CTRL+I key combination to invoke a shortcut saved in the browser settings,

      I have tried many methods, but no code wants to work.

      If I click my icon I see Alert with text, but the key combination does not work.

      icon.addEventListener('click', () => {
      
      var ciEvent = new KeyboardEvent("keydown", {
      
      key: "i",
      
      keyCode: 73,
      
      which: 73,
      
      code: "KeyI",
      
      location: 0,
      
      altKey: false,
      
      ctrlKey: true,
      
      metaKey: false,
      
      shiftKey: false,
      
      repeat: false
      
      });
      
      // get the active element and dispatch the events to it
      
      var activeElement = document.activeElement;
      
      if (activeElement) {
      
      activeElement.dispatchEvent(ciEvent);
      
      alert('WORK OK');
      
      }
      
      });
      
      
      ************************
      code execution:
      
      const event = new InputEvent('input', {
      
      inputType: 'insertText',
      
      data: 'i',
      
      isComposing: false
      
      });
      
      document.activeElement.dispatchEvent(event);
      
      console.log('InputEvent dispatched:', event);
      
      alert('WORK OK');
      
      return:
      _app-b1721212d74c071b.js:27 InputEvent dispatched: InputEvent{isTrusted: false, data: 'i', isComposing: false, inputType: 'insertText', dataTransfer: null,…}
      
      and sending keys doesn't work.
      
      After pressing the keys manually
      
      return:
      
      Keydown event caught: KeyboardEvent{isTrusted: true, key: 'Control', code: 'ControlLeft', location: 1, ctrlKey: true,…}
      
      _app-b1721212d74c071b.js:27 Keydown event caught: KeyboardEvent{isTrusted: true, key: 'i', code: 'KeyI', location: 0, ctrlKey: true,…}
      
      _app-b1721212d74c071b.js:27 Ctrl+I detected!
      

      It seems that the browser blocks the sending of keys with a script.

      Reply Quote 0
        burnout426 1 Reply Last reply
      • burnout426
        burnout426 Volunteer @MajesticF last edited by

        @MajesticF said in How send CTRL+I from Tampermonkey script.:

        It seems that the browser blocks the sending of keys with a script.

        Yeah, I think synthetic events like that are blocked. I don't even think that will work with direct JS with a script in a web page, so it's probably not a Tampermonkey-specific issue.

        Reply Quote 0
          MajesticF 1 Reply Last reply
        • MajesticF
          MajesticF @burnout426 last edited by

          @burnout426
          Strangely, a similar script that sends the ALT and ENTER keys to a specific page (not the browser in general) works without a problem. I don't know where the error is.

          Reply Quote 1
            1 Reply Last reply
          • First post
            Last post

          Computer browsers

          • Opera for Windows
          • Opera for Mac
          • Opera for Linux
          • Opera beta version
          • Opera USB

          Mobile browsers

          • Opera for Android
          • Opera Mini
          • Opera Touch
          • Opera for basic phones

          • Add-ons
          • Opera account
          • Wallpapers
          • Opera Ads

          • Help & support
          • Opera blogs
          • Opera forums
          • Dev.Opera

          • Security
          • Privacy
          • Cookies Policy
          • EULA
          • Terms of Service

          • About Opera
          • Press info
          • Jobs
          • Investors
          • Become a partner
          • Contact us

          Follow Opera

          • Opera - Facebook
          • Opera - Twitter
          • Opera - YouTube
          • Opera - LinkedIn
          • Opera - Instagram

          © Opera Software 1995-2025