Electron 注册全局快捷键(globalShortcut)

const app = require('app')
const globalShortcut = require('electron').globalShortcut
app.on('ready', () => {
  // Register a 'ctrl+x' shortcut listener.
  const ret = globalShortcut.register('ctrl+x', () => {
    console.log('ctrl+x is pressed')
  })
  if (!ret)
    console.log('registration failed')

  // Check whether a shortcut is registered.
  console.log(globalShortcut.isRegistered('ctrl+x'))
})
app.on('will-quit', () => {
  // Unregister a shortcut.
  globalShortcut.unregister('ctrl+x')
  // Unregister all shortcuts.
  globalShortcut.unregisterAll()
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

clipboard 剪切板事件 clipboard 模块以及 nativeImage 模块

const { clipboard, nativeImage } = require('electron')
clipboard.writeText('这是一个机器码')
console.log(clipboard.readText())
const img = nativeImage.createFromPath('static/favicon2.ico')
clipboard.writeImage(img)
const imgDataURL = clipboard.readImage().toDataURL()
const img3 = new Image()
img3.src = imgDataURL
document.body.appendChild(img3)
1
2
3
4
5
6
7
8
9