I'm building electron application that makes a desktop interface for a website. On the website, I already have registered keybindings, like Shift+?
that shows the table of all available keybindings.
I have to create an app menu of actions that correspond to the keybindings. Technically, when a user clicks on a menu item, it should trigger the keybinding on a webpage. Example:
When user press Help on the menu →
Shift+?
keybinding is being triggered → webpage detectskeypress
→ webpage shows the table of all registered keybindings appears.
The problem is that I can't send the Shift+?
keybinding to the webContents
. These attempts are failing:
webContents.sendInputEvent({type:'char', keyCode: 'Shift+?' });
webContents.sendInputEvent({type:'keydown', keyCode: 'Shift+?' });
webContents.sendInputEvent({type:'keyup', keyCode: 'Shift+?'});
How can I force the webpage to perform an action based on specific keybinding (a.k.a send keybinding to the webpage)?
UPD: I've found out few details. The order of events should be keyDown
, char
, keyUp
. I made a handy function for sending keybindings:
function sendKeybinding(win, keyCode) {
win.webContents.sendInputEvent({ type: 'keyDown', keyCode });
win.webContents.sendInputEvent({ type: 'char', keyCode });
win.webContents.sendInputEvent({ type: 'keyUp', keyCode });
}
The MenuItem
that uses this might look like this:
{
label: 'Show Keymap',
accelerator: ['Shift+/', '?'],
click(menuItem, browserWindow) {
sendKeybinding(browserWindow, '?');
},
}
Also, there is a hatch in a case with Shift+?
: ?
can be entered only with Shift
pressed, so actually, there is a two accelerators Shift+/
and ?
.
However, I still don't know how to enter the keybinding like CmdOrControl+B
.