Prevent input event from main process
This commit is contained in:
parent
5ae80d541b
commit
7a5ec96d53
2 changed files with 9 additions and 6 deletions
|
@ -4,7 +4,7 @@ const assert = require('assert')
|
|||
const path = require('path')
|
||||
const {closeWindow} = require('./window-helpers')
|
||||
|
||||
const {remote} = require('electron')
|
||||
const {ipcRenderer, remote} = require('electron')
|
||||
const {BrowserWindow, webContents, ipcMain} = remote
|
||||
|
||||
const isCi = remote.getGlobal('isCi')
|
||||
|
@ -102,16 +102,12 @@ describe('webContents module', function () {
|
|||
it('can prevent document keyboard events', (done) => {
|
||||
w.loadURL('file://' + path.join(__dirname, 'fixtures', 'pages', 'key-events.html'))
|
||||
w.webContents.once('did-finish-load', () => {
|
||||
w.webContents.once('before-input-event', (event, input) => {
|
||||
assert.equal(input.key, 'a')
|
||||
event.preventDefault()
|
||||
})
|
||||
|
||||
ipcMain.once('keydown', (event, key) => {
|
||||
assert.equal(key, 'b')
|
||||
done()
|
||||
})
|
||||
|
||||
ipcRenderer.send('prevent-next-input-event', 'a', w.webContents.id)
|
||||
w.webContents.sendInputEvent({type: 'keyDown', keyCode: 'a'})
|
||||
w.webContents.sendInputEvent({type: 'keyDown', keyCode: 'b'})
|
||||
})
|
||||
|
|
|
@ -8,6 +8,7 @@ const ipcMain = electron.ipcMain
|
|||
const dialog = electron.dialog
|
||||
const BrowserWindow = electron.BrowserWindow
|
||||
const protocol = electron.protocol
|
||||
const webContents = electron.webContents
|
||||
const v8 = require('v8')
|
||||
|
||||
const Coverage = require('electabul').Coverage
|
||||
|
@ -184,6 +185,12 @@ app.on('ready', function () {
|
|||
event.returnValue = 'done'
|
||||
})
|
||||
|
||||
ipcMain.on('prevent-next-input-event', (event, key, id) => {
|
||||
webContents.fromId(id).once('before-input-event', (event, input) => {
|
||||
if (key === input.key) event.preventDefault()
|
||||
})
|
||||
})
|
||||
|
||||
ipcMain.on('executeJavaScript', function (event, code, hasCallback) {
|
||||
if (hasCallback) {
|
||||
window.webContents.executeJavaScript(code, (result) => {
|
||||
|
|
Loading…
Add table
Reference in a new issue