Explorar el Código

Reset key state when window gains focus

When the window gains focus, reset the up/down state of all keys. Fixes #19.
Bentley James Oakes hace 7 años
padre
commit
609798cdb2
Se han modificado 1 ficheros con 11 adiciones y 0 borrados
  1. 11 0
      client/window_event.js

+ 11 - 0
client/window_event.js

@@ -211,6 +211,16 @@ WindowEventHelper = function(){
 	this.isKeyPressed = function( keyCode ){
 		return currentKeys[ keyCode ] == 1;
 	};
+
+	/**
+	 * Called when the window gains focus
+	 * Resets all keys
+	 */
+	this.onfocus = function(){
+		for (let keyCode in currentKeys){
+			currentKeys[ keyCode ] = 0;
+		}
+	};
 	
 	/**
 	 * Initializes the default window behavior
@@ -223,6 +233,7 @@ WindowEventHelper = function(){
 		window.onmousemove = this.onMouseMove;
 		window.onkeydown = this.onKeyDown;
 		window.onkeyup = this.onKeyUp;
+		window.onfocus = this.onfocus;
 	};
 	
 	return this;