Fixes hotkeys in firefox and all browsers + international keyboards.

This commit is contained in:
Simon Howe
2016-04-07 14:02:19 +02:00
parent f1c58efebb
commit c452b68200

View File

@@ -22,10 +22,6 @@ import { showingDebugToolbar, toggleDebugToolbar,
DebugToolbar } from './debug-toolbar.js';
const ESC_KEY_CODE = 27;
const D_KEY_CODE = 68;
const Q_KEY_CODE = 81;
const RIGHT_ANGLE_KEY_IDENTIFIER = 'U+003C';
const LEFT_ANGLE_KEY_IDENTIFIER = 'U+003E';
const keyPressLog = debug('scope:app-key-press');
/* make sure these can all be shallow-checked for equality for PureRenderMixin */
@@ -65,12 +61,14 @@ export default class App extends React.Component {
super(props, context);
this.onChange = this.onChange.bind(this);
this.onKeyPress = this.onKeyPress.bind(this);
this.onKeyUp = this.onKeyUp.bind(this);
this.state = getStateFromStores();
}
componentDidMount() {
AppStore.addListener(this.onChange);
window.addEventListener('keyup', this.onKeyPress);
window.addEventListener('keypress', this.onKeyPress);
window.addEventListener('keyup', this.onKeyUp);
getRouter().start({hashbang: true});
if (!AppStore.isRouteSet()) {
@@ -80,22 +78,39 @@ export default class App extends React.Component {
getApiDetails();
}
componentWillUnmount() {
window.removeEventListener('keypress', this.onKeyPress);
window.removeEventListener('keyup', this.onKeyUp);
}
onChange() {
this.setState(getStateFromStores());
}
onKeyPress(ev) {
keyPressLog('onKeyPress', 'keyCode', ev.keyCode, ev);
onKeyUp(ev) {
// don't get esc in onKeyPress
if (ev.keyCode === ESC_KEY_CODE) {
hitEsc();
} else if (ev.keyIdentifier === RIGHT_ANGLE_KEY_IDENTIFIER) {
}
}
onKeyPress(ev) {
//
// keyup gives 'key'
// keypress gives 'char'
// Distinction is important for international keyboard layouts where there
// is often a different {key: char} mapping.
//
keyPressLog('onKeyPress', 'keyCode', ev.keyCode, ev);
const char = String.fromCharCode(ev.charCode);
if (char === '<') {
pinNextMetric(-1);
} else if (ev.keyIdentifier === LEFT_ANGLE_KEY_IDENTIFIER) {
} else if (char === '>') {
pinNextMetric(1);
} else if (ev.keyCode === Q_KEY_CODE) {
} else if (char === 'q') {
unpinMetric();
selectMetric(null);
} else if (ev.keyCode === D_KEY_CODE) {
} else if (char === 'd') {
toggleDebugToolbar();
this.forceUpdate();
}