Fix Ctrl+Q shortcut by intercepting BrowserView keyboard events #36

Merged
Copilot merged 3 commits from copilot/fix-control-q-shortcut into main 2025-11-18 12:11:07 -06:00
Copilot commented 2025-11-18 11:53:17 -06:00 (Migrated from github.com)

The Ctrl+Q shortcut fails to close embedded browser sessions because globalShortcut.register() doesn't receive events when BrowserView has focus—the view consumes keyboard input before it reaches the global handler.

Changes

main.js

  • Added before-input-event listener to BrowserView webContents to intercept Escape and Ctrl+Q before web content processes them
  • Handles both Control (Windows/Linux) and Command (macOS) modifiers

test_electron.js

  • Added assertions verifying before-input-event handler exists and captures both shortcuts

ARCHITECTURE.md

  • Documented why before-input-event is necessary for BrowserView keyboard shortcuts

Implementation

currentBrowserView.webContents.on('before-input-event', (event, input) => {
  if (input.type === 'keyDown' && input.key === 'Escape') {
    event.preventDefault();
    closeBrowserView();
  }
  
  if (input.type === 'keyDown' && input.key === 'q' && (input.control || input.meta)) {
    event.preventDefault();
    closeBrowserView();
  }
});

The handler fires before the web page receives input, allowing us to capture shortcuts regardless of what content is loaded in the embedded browser.

Original prompt

This section details on the original issue you should resolve

<issue_title>Control+Q shortcut is still not working</issue_title>
<issue_description>Control+Q shortcut is still not working</issue_description>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

The Ctrl+Q shortcut fails to close embedded browser sessions because `globalShortcut.register()` doesn't receive events when BrowserView has focus—the view consumes keyboard input before it reaches the global handler. ## Changes **main.js** - Added `before-input-event` listener to BrowserView webContents to intercept Escape and Ctrl+Q before web content processes them - Handles both Control (Windows/Linux) and Command (macOS) modifiers **test_electron.js** - Added assertions verifying `before-input-event` handler exists and captures both shortcuts **ARCHITECTURE.md** - Documented why `before-input-event` is necessary for BrowserView keyboard shortcuts ## Implementation ```javascript currentBrowserView.webContents.on('before-input-event', (event, input) => { if (input.type === 'keyDown' && input.key === 'Escape') { event.preventDefault(); closeBrowserView(); } if (input.type === 'keyDown' && input.key === 'q' && (input.control || input.meta)) { event.preventDefault(); closeBrowserView(); } }); ``` The handler fires before the web page receives input, allowing us to capture shortcuts regardless of what content is loaded in the embedded browser. <!-- START COPILOT CODING AGENT SUFFIX --> <details> <summary>Original prompt</summary> > > ---- > > *This section details on the original issue you should resolve* > > <issue_title>Control+Q shortcut is still not working</issue_title> > <issue_description>Control+Q shortcut is still not working</issue_description> > > ## Comments on the Issue (you are @copilot in this section) > > <comments> > </comments> > </details> - Fixes SluberskiHomeLab/castroix#35 <!-- START COPILOT CODING AGENT TIPS --> --- 💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more [Copilot coding agent tips](https://gh.io/copilot-coding-agent-tips) in the docs.
SluberskiHomeLab (Migrated from github.com) reviewed 2025-11-18 11:53:17 -06:00
Sign in to join this conversation.
No description provided.