be smarter at deciding if key or keycode gets used for keybinds

This commit is contained in:
HF 2021-01-27 22:10:06 +01:00
parent 2d0c31fac6
commit 41a9d7f334
2 changed files with 42 additions and 19 deletions

View File

@ -500,29 +500,43 @@ class PixelPlainterControls {
} }
const { store } = this; const { store } = this;
switch (event.key) { /*
* key location
*/
switch (event.code) {
case 'ArrowUp': case 'ArrowUp':
case 'w': case 'KeyW':
store.dispatch(moveNorth()); store.dispatch(moveNorth());
break; return;
case 'ArrowLeft': case 'ArrowLeft':
case 'a': case 'KeyA':
store.dispatch(moveWest()); store.dispatch(moveWest());
break; return;
case 'ArrowDown': case 'ArrowDown':
case 's': case 'KeyS':
store.dispatch(moveSouth()); store.dispatch(moveSouth());
break; return;
case 'ArrowRight': case 'ArrowRight':
case 'd': case 'KeyD':
store.dispatch(moveEast()); store.dispatch(moveEast());
break; return;
case 'KeyE':
store.dispatch(zoomIn());
return;
case 'KeyQ':
store.dispatch(zoomOut());
return;
default:
}
/*
* key char
*/
switch (event.key) {
case '+': case '+':
case 'e':
store.dispatch(zoomIn()); store.dispatch(zoomIn());
break; break;
case '-': case '-':
case 'q':
store.dispatch(zoomOut()); store.dispatch(zoomOut());
break; break;
case 'Control': case 'Control':

View File

@ -13,6 +13,7 @@ import {
notify, notify,
} from '../actions'; } from '../actions';
const usedKeys = ['g', 'h', 'x', 'm', 'r', 'p'];
function onKeyPress(event: KeyboardEvent) { function onKeyPress(event: KeyboardEvent) {
// ignore key presses if modal is open or chat is used // ignore key presses if modal is open or chat is used
@ -22,31 +23,39 @@ function onKeyPress(event: KeyboardEvent) {
return; return;
} }
switch (event.key) { /*
* if char of key isn't used by a keybind,
* we check if the key location is where a
* key that is used would be on QWERTY
*/
const key = (usedKeys.includes(event.key))
? event.key
: event.code.substr(-1).toLowerCase();
switch (key) {
case 'g': case 'g':
store.dispatch(toggleGrid()); store.dispatch(toggleGrid());
break; return;
case 'h': case 'h':
store.dispatch(toggleHistoricalView()); store.dispatch(toggleHistoricalView());
break; return;
case 'x': case 'x':
store.dispatch(togglePixelNotify()); store.dispatch(togglePixelNotify());
break; return;
case 'm': case 'm':
store.dispatch(toggleMute()); store.dispatch(toggleMute());
break; return;
case 'r': { case 'r': {
const state = store.getState(); const state = store.getState();
const { hover } = state.gui; const { hover } = state.gui;
const text = hover.join('_'); const text = hover.join('_');
copy(text); copy(text);
store.dispatch(notify('Copied!')); store.dispatch(notify('Copied!'));
break; return;
} }
case 'p': { case 'p':
store.dispatch(toggleHiddenCanvases()); store.dispatch(toggleHiddenCanvases());
break; break;
}
default: default:
} }
} }