electron快捷键

Just like in any other native desktop application, keyboard shortcuts save time and make it easy to perform some tasks like closing an app, copy, paste, zoom-in, cut, and more.

就像在其他任何本机桌面应用程序中一样, 键盘快捷键可以节省时间,并且可以轻松执行一些任务,例如关闭应用程序复制粘贴放大剪切等。

In this tutorial, we will add a keyboard shortcut that will accompany a menu item.

在本教程中,我们将添加一个与菜单项一起提供的键盘快捷键

That is, rather than clicking on that menu item, the user can simply press the keyboard shortcut and the task will be done.

也就是说,用户无需单击该菜单项,只需按下键盘快捷键即可完成任务。

This tutorial only handles keyboard shortcuts. If you're new in Electron JS, consider checking my recent articles under the list of articles.

本教程仅处理键盘快捷键 。 如果您是Electron JS的新手,请考虑在文章列表下查看我最近的文章。

In Electron JS, accelerators are responsible for adding keyboard shortcuts. Below is the definition of accelerators from their official documentation:

在Electron JS中, 加速器负责添加键盘快捷键。 以下是加速器官方文档中的定义:

Accelerators are Strings that can contain multiple modifiers and a single key code, combined by the + character, and are used to define keyboard shortcuts throughout your application.

加速器是可以包含多个修饰符和单个键代码(由+字符组合)的字符串,用于在整个应用程序中定义键盘快捷键。

For example, CTRL+A, CTRL+SHIFT+Q

例如, CTRL + ACTRL + SHIFT + Q

Where "CTRL" is the modifier and "Q" is the key code.

其中“ CTRL”是修饰语, “ Q”是键控代码。

The full list of modifiers and key codes can be found on their official documentation.

修饰符和键控代码的完整列表可以在其官方文档中找到。

Open your main JavaScript file and type the following,

打开您的主要JavaScript文件,然后键入以下内容:

const electron = require ('electron')
const app = electron.app // electron module
const BrowserWindow = electron.BrowserWindow //enables UI
const Menu = electron.Menu // menu module
let win
app.on('ready', _ => {win = new BrowserWindow({width: 800,
height: 600,
})
const template = [
{label: 'Help',   // Help menu item
submenu: [{ // adds submenu items
label: 'About US',
},{label: 'Quit',
role: 'quit', // gives this menu the role to close app when clicked
accelerator: 'Ctrl+Q'  // creates a shortcut to this action
}]
}
]
// sets the menu
const menu = Menu.buildFromTemplate (template)
Menu.setApplicationMenu (menu)
})

You can see the way the shortcut is written beside the submenu just like in any other native desktop application. On pressing the above command, watch as the app closes.

您可以像在其他任何本机桌面应用程序中一样,在子菜单旁边看到编写快捷方式的方式。 按下上面的命令后,请注意该应用程序是否关闭。

Shortcuts can also be added using the globalShortcut module using the register method.

也可以使用register方法使用globalShortcut模块添加快捷方式。

Example: In the same file above modify the code;

示例:在上面的同一文件中修改代码;

const electron = require ('electron')
const {globalShortcut } = require('electron')
const app = electron.app // electron module
const BrowserWindow = electron.BrowserWindow //enables UI
const Menu = electron.Menu // menu module
let win
app.on('ready', _ => {win = new BrowserWindow({width: 800,
height: 600,
})
globalShortcut.register('Control+L', () => {  // creates a global shortcut
console.log ('gobal shortcut presses')      // action when shortcut is pressed
})
const template = [
{label: 'Help',   // Help menu item
submenu: [{                             // adds submenu items
label: `About US`,
},{label: 'Quit',
role: 'quit',            // gives this menu the role to close app when clicked
accelerator: 'Ctrl+Q'   // creates a shortcut to this action
}]
}
]
const menu = Menu.buildFromTemplate (template)     // sets the menu
Menu.setApplicationMenu (menu)
})

Whenever "CTRL+L" is pressed, that statement is logged or printed out on the console as seen above

每当按下“ CTRL + L”时 ,该语句就会被记录或打印在控制台上,如上所示

Thanks for reading.

谢谢阅读。

Drop your comments if in need of help.

如果需要帮助,请删除您的评论。

翻译自: https://www.includehelp.com/electron-js/add-keyboard-shortcuts-in-electron-js-application.aspx

electron快捷键

electron快捷键_如何在Electron JS应用程序中添加键盘快捷键?相关推荐

  1. node.js ejs_如何在Node.js应用程序中使用EJS模板

    node.js ejs by Jennifer Bland 詹妮弗·布兰德(Jennifer Bland) 如何在Node.js应用程序中使用EJS模板 (How to use EJS Templat ...

  2. rethinkdb_如何在Node.js应用程序中使用RethinkDB

    rethinkdb 这篇文章是由同行评审Agbonghama柯林斯和马丁·马丁内斯 . 感谢所有SitePoint的同行评审员使SitePoint内容达到最佳状态! Web应用程序最常见的任务之一就是 ...

  3. node.js api接口_如何在Node.js API客户端中正常处理故障

    node.js api接口 by Roger Jin 罗杰·金(Roger Jin) 如何在Node.js API客户端中正常处理故障 (How to gracefully handle failur ...

  4. angular2创建应用_如何在Angular 2+应用程序中使用JavaScript库

    angular2创建应用 Do you remember when you were learning AngularJS (version 1), and tutorials kept tellin ...

  5. 如何在Node.js应用程序中使用RethinkDB

    这篇文章是由同行评审Agbonghama柯林斯和马丁·马丁内斯 . 感谢所有SitePoint的同行评审人员使SitePoint内容达到最佳状态! Web应用程序最常见的任务之一就是保存数据. 没有存 ...

  6. 敏捷中gwt含义_在您的GWT应用程序中添加JSON功能

    敏捷中gwt含义 JSON简介 在Web应用程序上工作时,总是会出现客户端-服务器数据交换的问题. 在此问题上有多种方法,其中许多使用XML进行交换. 执行此任务的一种不太知名的格式是JSON. JS ...

  7. electron 菜单栏_如何在Electron JS中添加任务栏图标菜单?

    electron 菜单栏 If you are new here, please consider checking out my recent articles on Electron JS inc ...

  8. win10屏幕快照快捷键_如何在Windows 8和10中更改默认屏幕快照文件夹的位置

    win10屏幕快照快捷键 Windows redesigned its screenshot feature in Windows 8, and you no longer need to launc ...

  9. ubuntu添加面板_如何在Ubuntu的顶部面板中添加天气信息

    ubuntu添加面板 Modern operating systems offer weather information out-of-the-box. There's Windows 10's w ...

最新文章

  1. 在wamp环境下面安装Zend Optimizer的方法
  2. Java 增强型的for循环 for each
  3. WinCE 和Win Mobile的关系
  4. @Transactional事务几点注意
  5. 阿里钉钉陈航发布10亿“春雨计划”,推进企业级市场服务创新
  6. matlab_simulink笔记01——模块属性的设置以及模块参数的设置
  7. android 动画 返回,Android“菜单图标变返回”动画
  8. iOS程序UI主线程和定时器相互阻塞的问题
  9. [MySQL优化案例]系列 -- DISABLE/ENABLE KEYS的作用
  10. linux分布式安装hadoop1.2
  11. [转载] python(numpy) 实现神经网络训练 (卷积 全连接 池化)
  12. vulkan android 三星,vulkan android
  13. htmL全栈开发项目实例,【译】基于MEAN的全栈开发实例教程6(完)
  14. 40个好用的Unity游戏开发插件大合集
  15. 如何从零开始搭建SRE?
  16. 高性能网站架构之缓存篇—Redis集群搭建
  17. 眼球追踪技术给各大科技巨头带来的四大应用前景
  18. 美国程序员把工作外包给中国程序员,啥也不干年入 20 万美元,这操作也是骚...
  19. 图库/相册/播放器看不到迅雷下载的视频的解决办法
  20. 霍金门徒:计算机如何比人更懂世界

热门文章

  1. Xtrabackup介绍与原理
  2. 双屏玩游戏鼠标滑到另外一个屏幕_灵耀X2 Duo双屏操作!边上课边做笔记秀了,旦用难回的创新体验...
  3. 路边烟酒店明明没什么客人,为什么一年还能赚几十万?原因很简单
  4. js 跳转链接的几种方式
  5. impdp导入指定表 oracle_oracle 11g 将表导入到其他用户的impdp命令
  6. 22年1.17 入门 MarkDown语法 (+号不需要打出来)
  7. java 半个汉字,Java截取字符串军令状汉字不被截取半个
  8. [附源码]java毕业设计商务酒店管理系统
  9. 创意个人头像信息卡片js特效
  10. C语言 计算猴子摘桃数目(多种方法)