From c4fcf43b3ce90bbe0f141f65af7a913e94b3d0f7 Mon Sep 17 00:00:00 2001 From: James Barnsley Date: Thu, 27 Oct 2016 13:56:47 +1300 Subject: [PATCH] Slick handling of keyboard shortcuts --- src/js/views/App.js | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/js/views/App.js b/src/js/views/App.js index 427cd1bd..e7e3b31e 100755 --- a/src/js/views/App.js +++ b/src/js/views/App.js @@ -20,20 +20,42 @@ class App extends React.Component{ constructor(props){ super(props); - this.handleKeyUp = this.handleKeyUp.bind(this); + this.handleKeyDown = this.handleKeyDown.bind(this); } componentWillMount(){ this.props.mopidyActions.connect(); window.addEventListener("keyup", this.handleKeyUp, false); + window.addEventListener("keydown", this.handleKeyDown, false); } componentWillUnmount(){ window.removeEventListener("keyup", this.handleKeyUp, false); + window.removeEventListener("keydown", this.handleKeyDown, false); + } + + shouldTriggerShortcut(e){ + var ignoreNodes = ['INPUT', 'TEXTAREA']; + var keyCodes = [32]; + + if( ignoreNodes.indexOf(e.target.nodeName) > -1 ){ + return false; + } + + if( keyCodes.indexOf(e.keyCode) > -1 ){ + e.preventDefault(); + return true; + } + } + + handleKeyDown(e){ + this.shouldTriggerShortcut(e); } handleKeyUp(e){ + if( !this.shouldTriggerShortcut(e) ) return; + switch(e.keyCode){ case 32: // spacebar if( this.props.mopidy.state == 'playing' ){