pixelplanet/src/controls/keypress.js

64 lines
1.3 KiB
JavaScript
Raw Normal View History

/*
* keypress actions
* @flow
*/
import store from '../ui/store';
import copy from '../utils/clipboard';
import {
toggleGrid,
2020-06-23 16:49:56 +00:00
toggleHistoricalView,
toggleHiddenCanvases,
togglePixelNotify,
toggleMute,
notify,
} from '../actions';
const usedKeys = ['g', 'h', 'x', 'm', 'r', 'p'];
function onKeyPress(event: KeyboardEvent) {
// ignore key presses if modal is open or chat is used
if (event.target.nodeName === 'INPUT'
|| event.target.nodeName === 'TEXTAREA'
) {
return;
}
/*
* 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':
store.dispatch(toggleGrid());
return;
2020-06-23 16:49:56 +00:00
case 'h':
store.dispatch(toggleHistoricalView());
return;
case 'x':
store.dispatch(togglePixelNotify());
return;
case 'm':
store.dispatch(toggleMute());
return;
2020-04-29 02:38:36 +00:00
case 'r': {
const state = store.getState();
const { hover } = state.gui;
const text = hover.join('_');
copy(text);
store.dispatch(notify('Copied!'));
return;
}
case 'p':
store.dispatch(toggleHiddenCanvases());
break;
default:
}
}
export default onKeyPress;