Code/JavaScript

[Electron] 일렉트론 메뉴, 단축키 사용법

codens 2020. 11. 27. 17:12

- 주의! Electron v12(2020/11 현재 베타) 에서 메뉴 작동 문제 있음 , v11 사용 권장

 

//------------------

// main.js 파일

 

const { app, Menu, BrowserWindow, globalShortcut } = require("electron");

 

// 메뉴 설정

const menu = require("./app/menu.js");

Menu.setApplicationMenu(Menu.buildFromTemplate(menu));



app.whenReady().then(() => {

    // 전역 단축키 설정   

   globalShortcut.register("F11", () => {

        let browser = BrowserWindow.getFocusedWindow();

        //browser.isDevToolsOpened() ? browser.closeDevTools() : browser.openDevTools();

        browser.toggleDevTools();

    });

 

    createWindow();

 

  //UI 에서 키보드 이벤트 감지(로컬키)

    BrowserWindow.getFocusedWindow().webContents.on('before-input-event', (eventinput=> {

        console.log('키 'input.controlinput.key);

    })

});



//--------------------------------

//메뉴 설정

// app/menu.js 파일

 

const { app, Menu } = require("electron");

 

const isMac = process.platform === "darwin";

const menu_template = [

    // { role: 'appMenu' }

    ...(isMac

        ? [

            {

                label: app.name,

                submenu: [

                    { role: "about" },

                    { type: "separator" },

                    { role: "services" },

                    { type: "separator" },

                    { role: "hide" },

                    { role: "hideothers" },

                    { role: "unhide" },

                    { type: "separator" },

                    { role: "quit" },

                ],

            },

        ]

        : []),

    // { role: 'fileMenu' }

    {

        label: "File",

        submenu: [isMac ? { role: "close" } : { role: "quit" }],

    },

    // { role: 'editMenu' }

    {

        label: "Edit",

        submenu: [

            { role: "undo" },

            { role: "redo" },

            { type: "separator" },

            { role: "cut" },

            { role: "copy" },

            { role: "paste" },

            ...(isMac

                ? [

                    { role: "pasteAndMatchStyle" },

                    { role: "delete" },

                    { role: "selectAll" },

                    { type: "separator" },

                    {

                        label: "Speech",

                        submenu: [

                            { role: "startspeaking" },

                            { role: "stopspeaking" },

                        ],

                    },

                ]

                : [

                    { role: "delete" },

                    { type: "separator" },

                    { role: "selectAll" },

                ]),

        ],

    },

    // { role: 'viewMenu' }

    {

        label: "View",

        submenu: [

            { role: "reload" },

            { role: "forcereload" },           

            {

                role: "toggledevtools",

                accelerator: "F12",//로컬 단축키

            },

            { type: "separator" },

            { role: "resetzoom" },

            { role: "zoomin" },

            { role: "zoomout" },

            { type: "separator" },

            { role: "togglefullscreen" },

        ],

    },

    // { role: 'windowMenu' }

    {

        label: "Window",

        submenu: [

            { role: "minimize" },

            { role: "zoom" },

            ...(isMac

                ? [

                    { type: "separator" },

                    { role: "front" },

                    { type: "separator" },

                    { role: "window" },

                ]

                : [{ role: "close" }]),

        ],

    },

    {

        role: "help",

        submenu: [

            {

                label: "Learn More1",

                accelerator: "CmdOrCtrl+P",

                click: async () => {

                    //const { shell } = require("electron");

                    //await shell.openExternal("https://electronjs.org");

                    console.log("test");

                },

            },

        ],

    },

];

module.exports = menu_template;



반응형