11var registerComponent = require ( '../core/component' ) . registerComponent ;
2+ var shouldCaptureKeyEvent = require ( '../utils/' ) . shouldCaptureKeyEvent ;
23var THREE = require ( '../lib/three' ) ;
34
45var MAX_DELTA = 0.2 ;
@@ -24,8 +25,12 @@ module.exports.Component = registerComponent('wasd-controls', {
2425 this . velocity = new THREE . Vector3 ( ) ;
2526 // To keep track of the pressed keys
2627 this . keys = { } ;
28+ this . onBlur = this . onBlur . bind ( this ) ;
29+ this . onFocus = this . onFocus . bind ( this ) ;
30+ this . onVisibilityChange = this . onVisibilityChange . bind ( this ) ;
2731 this . onKeyDown = this . onKeyDown . bind ( this ) ;
2832 this . onKeyUp = this . onKeyUp . bind ( this ) ;
33+ this . attachVisibilityEventListeners ( ) ;
2934 } ,
3035
3136 update : function ( previousData ) {
@@ -81,11 +86,12 @@ module.exports.Component = registerComponent('wasd-controls', {
8186 } ,
8287
8388 play : function ( ) {
84- this . attachEventListeners ( ) ;
89+ this . attachKeyEventListeners ( ) ;
8590 } ,
8691
8792 pause : function ( ) {
88- this . removeEventListeners ( ) ;
93+ this . keys = { } ;
94+ this . removeKeyEventListeners ( ) ;
8995 } ,
9096
9197 tick : function ( t ) {
@@ -94,25 +100,54 @@ module.exports.Component = registerComponent('wasd-controls', {
94100
95101 remove : function ( ) {
96102 this . pause ( ) ;
103+ this . removeVisibilityEventListeners ( ) ;
104+ } ,
105+
106+ attachVisibilityEventListeners : function ( ) {
107+ window . addEventListener ( 'blur' , this . onBlur ) ;
108+ window . addEventListener ( 'focus' , this . onFocus ) ;
109+ document . addEventListener ( 'visibilitychange' , this . onVisibilityChange ) ;
97110 } ,
98111
99- attachEventListeners : function ( ) {
100- // Keyboard events
101- window . addEventListener ( 'keydown ', this . onKeyDown , false ) ;
102- window . addEventListener ( 'keyup ', this . onKeyUp , false ) ;
112+ removeVisibilityEventListeners : function ( ) {
113+ window . removeEventListener ( 'blur' , this . onBlur ) ;
114+ window . removeEventListener ( 'focus ', this . onFocus ) ;
115+ document . removeEventListener ( 'visibilitychange ', this . onVisibilityChange ) ;
103116 } ,
104117
105- removeEventListeners : function ( ) {
106- // Keyboard events
118+ attachKeyEventListeners : function ( ) {
119+ window . addEventListener ( 'keydown' , this . onKeyDown ) ;
120+ window . addEventListener ( 'keyup' , this . onKeyUp ) ;
121+ } ,
122+
123+ removeKeyEventListeners : function ( ) {
107124 window . removeEventListener ( 'keydown' , this . onKeyDown ) ;
108125 window . removeEventListener ( 'keyup' , this . onKeyUp ) ;
109126 } ,
110127
128+ onBlur : function ( ) {
129+ this . pause ( ) ;
130+ } ,
131+
132+ onFocus : function ( ) {
133+ this . play ( ) ;
134+ } ,
135+
136+ onVisibilityChange : function ( ) {
137+ if ( document . hidden ) {
138+ this . onBlur ( ) ;
139+ } else {
140+ this . onFocus ( ) ;
141+ }
142+ } ,
143+
111144 onKeyDown : function ( event ) {
145+ if ( ! shouldCaptureKeyEvent ( event ) ) { return ; }
112146 this . keys [ event . keyCode ] = true ;
113147 } ,
114148
115149 onKeyUp : function ( event ) {
150+ if ( ! shouldCaptureKeyEvent ( event ) ) { return ; }
116151 this . keys [ event . keyCode ] = false ;
117152 } ,
118153
0 commit comments