fix: support the Command key on mac

This commit is contained in:
Matei Adriel 2020-04-15 17:38:45 +03:00
parent 201ecf82b4
commit 5667b1652a
2 changed files with 14 additions and 8 deletions

View file

@ -27,6 +27,13 @@ export class KeyboardInput {
*/ */
private subscription: Array<Subscription> = [] private subscription: Array<Subscription> = []
/**
* Check if a key is pressed
*/
private isPressed(key: string, event: KeyboardEvent) {
return (key === 'ctrl' && event.metaKey) || keycode(event) === key
}
/** /**
* use for keyboard events * use for keyboard events
* @param params the keys to listen to * @param params the keys to listen to
@ -37,7 +44,7 @@ export class KeyboardInput {
//push a new subscription to the subscriptions array //push a new subscription to the subscriptions array
this.subscription.push( this.subscription.push(
fromEvent(document, 'keydown').subscribe(e => { fromEvent(document, 'keydown').subscribe((e: KeyboardEvent) => {
//remember the length of the pressed array //remember the length of the pressed array
//used to see if anything changed //used to see if anything changed
const last = this.pressed.length const last = this.pressed.length
@ -45,7 +52,7 @@ export class KeyboardInput {
//if the key is pressed and it isnt already pressed, //if the key is pressed and it isnt already pressed,
//then add it to the pressed array //then add it to the pressed array
for (let i of this.keys) for (let i of this.keys)
if (i == keycode(e) && this.pressed.indexOf(i) == -1) if (this.isPressed(i, e) && this.pressed.indexOf(i) == -1)
this.pressed.push(i) this.pressed.push(i)
//if there was no key pressd before, and now there is //if there was no key pressd before, and now there is
@ -59,7 +66,7 @@ export class KeyboardInput {
//push a new subscription to the subscriptions array //push a new subscription to the subscriptions array
this.subscription.push( this.subscription.push(
fromEvent(document, 'keyup').subscribe(e => { fromEvent(document, 'keyup').subscribe((e: KeyboardEvent) => {
//remember the length of the pressed array //remember the length of the pressed array
//used to see if anything changed //used to see if anything changed
const last = this.pressed.length const last = this.pressed.length
@ -68,7 +75,7 @@ export class KeyboardInput {
//if the key is released and it was pressed, //if the key is released and it was pressed,
//then remove it from the pressed array //then remove it from the pressed array
for (let i of this.keys) for (let i of this.keys)
if (i == keycode(e) && this.pressed.indexOf(i) != -1) if (this.isPressed(i, e) && this.pressed.indexOf(i) != -1)
this.pressed.splice(this.pressed.indexOf(i), 1) this.pressed.splice(this.pressed.indexOf(i), 1)
//if there was at least a key pressd before, and now there isnt //if there was at least a key pressd before, and now there isnt
@ -86,7 +93,7 @@ export class KeyboardInput {
* ends the listening * ends the listening
*/ */
public dispose() { public dispose() {
this.subscription.forEach(e => e.unsubscribe()) this.subscription.forEach((e) => e.unsubscribe())
this.value = false this.value = false
this.valueChanges.next(false) this.valueChanges.next(false)
this.valueChanges.complete() this.valueChanges.complete()

View file

@ -1,13 +1,12 @@
import { KeyboardInput } from '../classes/KeyboardInput' import { KeyboardInput } from '../classes/KeyboardInput'
import { KeyBindingMap, KeyBinding } from '../types/KeyBindingMap' import { KeyBindingMap, KeyBinding } from '../types/KeyBindingMap'
import { SidebarActions } from '../../simulation-actions/constants' import { SidebarActions } from '../../simulation-actions/constants'
import { SidebarAction } from '../../simulation-actions/types/SidebarAction'
import { modalIsOpen } from '../../modals/helpers/modalIsOpen' import { modalIsOpen } from '../../modals/helpers/modalIsOpen'
export const listeners: Record<string, KeyboardInput> = {} export const listeners: Record<string, KeyboardInput> = {}
const keyBindings = Object.values(SidebarActions) const keyBindings = Object.values(SidebarActions)
.filter(action => action.keybinding) .filter((action) => action.keybinding)
.map( .map(
(action): KeyBinding => { (action): KeyBinding => {
return { return {
@ -30,7 +29,7 @@ export const initKeyBindings = (bindings: KeyBindingMap = keyBindings) => {
listeners[key] = new KeyboardInput(key) listeners[key] = new KeyboardInput(key)
} }
window.addEventListener('keydown', e => { window.addEventListener('keydown', (e) => {
if (!modalIsOpen() && location.pathname === '/') { if (!modalIsOpen() && location.pathname === '/') {
const current: { const current: {
keys: string[] keys: string[]