Skip to content
/ bk2ea Public

An util function for converting keycode in the browser to Accelerator in Electron.

Notifications You must be signed in to change notification settings

hcfyapp/bk2ea

Repository files navigation

bk2ea

An util function for converting keycode in the browser to Accelerator in Electron.

Usage

First, install it:

npm i @hcfy/bk2ea

Then:

import { convert } from '@hcfy/bk2ea'

document.addEventListener('keydown', (e) => {
  const a = convert(e.code)
  if (a) {
    console.log('Electron Accelerator', a)
  } else {
    console.log("Electron doesn't support this key:", e.code)
  }
})

A note for CommandOrControl

// If the second parameter is not passed
convert('MetaLeft') === 'Meta'
convert('ControlLeft') === 'Control'

const macOS = navigator.userAgent.includes('Mac')

// If you press these two keys in macOS
macOS === true
convert('MetaLeft', macOS) === 'CommandOrControl'
convert('ControlLeft', macOS) === 'Control'

// If you press these two keys in Windows or Linux
macOS === false
convert('MetaLeft', macOS) === 'Meta'
convert('ControlLeft', macOS) === 'CommandOrControl'

About

An util function for converting keycode in the browser to Accelerator in Electron.

Resources

Stars

Watchers

Forks