2017-02-25 18:59:13 -08:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								# Keyboard Shortcuts
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								>  Configure local and global keyboard shortcuts
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								## Local Shortcuts
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								You can use the [Menu] module to configure keyboard shortcuts that will
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								be triggered only when the app is focused. To do so, specify an
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								[`accelerator` ] property when creating a [MenuItem].
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								const {Menu, MenuItem} = require('electron')
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								const menu = new Menu()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								menu.append(new MenuItem({
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  label: 'Print',
							 
						 
					
						
							
								
									
										
										
										
											2017-02-27 09:34:35 -08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								  accelerator: 'CmdOrCtrl+P',
							 
						 
					
						
							
								
									
										
										
										
											2017-02-25 18:59:13 -08:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								  click: () => { console.log('time to print stuff') }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								}))
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								It's easy to configure different key combinations based on the user's operating system.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								{
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  accelerator: process.platform === 'darwin' ? 'Alt+Cmd+I' : 'Ctrl+Shift+I'
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-02-25 19:21:33 -08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								## Global Shortcuts
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								You can use the [globalShortcut] module to detect keyboard events even when
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								the application does not have keyboard focus.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								const {app, globalShortcut} = require('electron')
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								app.on('ready', () => {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  globalShortcut.register('CommandOrControl+X', () => {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    console.log('CommandOrControl+X is pressed')
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  })
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								})
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-02-25 19:07:09 -08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								## Shortcuts within a BrowserWindow
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-02-25 18:59:13 -08:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								If you want to handle keyboard shortcuts for a [BrowserWindow], you can use the `keyup`  and `keydown`  event listeners on the window object inside the renderer process.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								window.addEventListener('keyup', doSomething, true)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								Note the third parameter `true`  which means the listener will always receive key presses before other listeners so they can't have `stopPropagation()`  called on them.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-06-25 17:36:38 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								The [`before-input-event` ](web-contents.md#event-before-input-event ) event
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								is emitted before dispatching `keydown`  and `keyup`  events in the page. It can
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								be used to catch and handle custom shortcuts that are not visible in the menu.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-02-25 19:07:09 -08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								If you don't want to do manual shortcut parsing there are libraries that do advanced key detection such as [mousetrap].
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```js
							 
						 
					
						
							
								
									
										
										
										
											2017-02-27 09:34:35 -08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								Mousetrap.bind('4', () => { console.log('4') })
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								Mousetrap.bind('?', () => { console.log('show shortcuts!') })
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								Mousetrap.bind('esc', () => { console.log('escape') }, 'keyup')
							 
						 
					
						
							
								
									
										
										
										
											2017-02-25 19:07:09 -08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								// combinations
							 
						 
					
						
							
								
									
										
										
										
											2017-02-27 09:34:35 -08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								Mousetrap.bind('command+shift+k', () => { console.log('command shift k') })
							 
						 
					
						
							
								
									
										
										
										
											2017-02-25 19:07:09 -08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								// map multiple combinations to the same callback
							 
						 
					
						
							
								
									
										
										
										
											2017-02-27 09:34:35 -08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								Mousetrap.bind(['command+k', 'ctrl+k'], () => {
							 
						 
					
						
							
								
									
										
										
										
											2017-02-25 19:07:09 -08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								  console.log('command k or control k')
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-02-27 09:34:35 -08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								  // return false to prevent default behavior and stop event from bubbling
							 
						 
					
						
							
								
									
										
										
										
											2017-02-25 19:07:09 -08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								  return false
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								})
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								// gmail style sequences
							 
						 
					
						
							
								
									
										
										
										
											2017-02-27 09:34:35 -08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								Mousetrap.bind('g i', () => { console.log('go to inbox') })
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								Mousetrap.bind('* a', () => { console.log('select all') })
							 
						 
					
						
							
								
									
										
										
										
											2017-02-25 19:07:09 -08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								// konami code!
							 
						 
					
						
							
								
									
										
										
										
											2017-02-27 09:34:35 -08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								Mousetrap.bind('up up down down left right left right b a enter', () => {
							 
						 
					
						
							
								
									
										
										
										
											2017-02-25 19:07:09 -08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								  console.log('konami code')
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								})
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-02-25 18:59:13 -08:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								[Menu]: ../api/menu.md
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								[MenuItem]: ../api/menu-item.md
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								[globalShortcut]: ../api/global-shortcut.md
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								[`accelerator` ]: ../api/accelerator.md
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								[BrowserWindow]: ../api/browser-window.md
							 
						 
					
						
							
								
									
										
										
										
											2017-02-25 19:07:09 -08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								[mousetrap]: https://github.com/ccampbell/mousetrap