浏览代码

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 7 年之前
父节点
当前提交
609798cdb2
共有 1 个文件被更改,包括 11 次插入0 次删除
  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;