2016-05-14 84 views
1

在Electron中,我有下面的腳本是我的main.js,它是恢復窗口並將其聚焦的快捷方式,但是我想將輸入集中在窗口內。我如何實現這一目標?電子:我如何與瀏覽器窗口進行通信?

main.js

app.on('ready', function(){ 
    createWindow(); 
    var ret = globalShortcut.register('Command+Control+Alt+G', function() { 
    mainWindow.restore(); 
    mainWindow.focus(); 
    $('.js-search').focus(); 
    }); 
}) 

回答

4

你可以從你的主要過程將消息發送到使用webContents渲染過程中,在頁面的焦點。

頁面(渲染器進程)可以偵聽該消息,然後聚焦該元素本身。

主要工藝

globalShortcut.register('Command+Control+Alt+G', function() { 

    win.restore(); 
    win.focus(); 
    win.webContents.send('focus-element', '.js-search'); 
}); 

渲染處理(頁面)

<script> 
    const {ipcRenderer} = require('electron'); 

    ipcRenderer.on('focus-element', (event, selector) => { 
     document.querySelector(selector).focus(); 
    }); 
</script> 
+0

工作非常出色。謝謝馬特。 – Rich

+0

真棒:)沒問題。 –

相關問題