- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在学习 JS,因此我认为与其使用键盘库,不如自己制作一个键盘库,这样我可以更好地了解它是如何工作的。我正在创建一个游戏,它实际上并不是一个仅在按下键盘时更新游戏的选项,因此我创建了一个循环来运行库,我将使用它来更新玩家位置。问题是它不断崩溃,我认为这与创建和关闭事件太多有关。
运行库的相关代码将返回一个 key :
function Keyboard(){
ArcKeyboard.ActiveKeys();
};
setInterval(function(){Keyboard()},200);
图书馆:
var ArcKeyboard
(function(ArcKeyboard){
var Key = {
1: ["No Key Assigned",0],
2: ["No Key Assigned",0],
3: ["No Key Assigned",0],
4: ["No Key Assigned",0],
5: ["No Key Assigned",0],
6: ["No Key Assigned",0],
7: ["No Key Assigned",0],
8: ["backspace",0],
9: ["tab",0],
10: ["No Key Assigned",0],
11: ["No Key Assigned",0],
12: ["No Key Assigned",0],
13: ["enter",0],
14: ["No Key Assigned",0],
15: ["No Key Assigned",0],
16: ["shift",0],
17: ["ctrl",0],
18: ["alt",0],
19: ["pause",0],
20: ["capslock",0],
21: ["No Key Assigned",0],
22: ["No Key Assigned",0],
23: ["No Key Assigned",0],
24: ["No Key Assigned",0],
25: ["No Key Assigned",0],
26: ["No Key Assigned",0],
27: ["esc",0],
28: ["No Key Assigned",0],
29: ["No Key Assigned",0],
30: ["No Key Assigned",0],
31: ["No Key Assigned",0],
32: ["space",0],
33: ["pageup",0],
34: ["pagedown",0],
35: ["end",0],
36: ["home",0],
37: ["left",0],
38: ["up",0],
39: ["right",0],
40: ["down",0],
41: ["No Key Assigned",0],
42: ["No Key Assigned",0],
43: ["No Key Assigned",0],
44: ["No Key Assigned",0],
45: ["insert",0],
46: ["delete",0],
47: ["No Key Assigned",0],
48: ["0",0],
49: ["1",0],
50: ["2",0],
51: ["3",0],
52: ["4",0],
53: ["5",0],
54: ["6",0],
55: ["7",0],
56: ["8",0],
57: ["9",0],
58: ["No Key Assigned",0],
59: ["No Key Assigned",0],
60: ["No Key Assigned",0],
61: ["No Key Assigned",0],
62: ["No Key Assigned",0],
63: ["No Key Assigned",0],
64: ["No Key Assigned",0],
65: ["a",0],
66: ["b",0],
67: ["c",0],
68: ["d",0],
69: ["e",0],
70: ["f",0],
71: ["g",0],
72: ["h",0],
73: ["i",0],
74: ["j",0],
75: ["k",0],
76: ["l",0],
77: ["m",0],
78: ["n",0],
79: ["o",0],
80: ["p",0],
81: ["q",0],
82: ["r",0],
83: ["s",0],
84: ["t",0],
85: ["u",0],
86: ["v",0],
87: ["w",0],
88: ["x",0],
89: ["y",0],
90: ["z",0],
91: ["windowskey",0],
92: ["No Key Assigned",0],
93: ["menukey",0],
94: ["No Key Assigned",0],
95: ["No Key Assigned",0],
96: ["keypad0",0],
97: ["keypad1",0],
98: ["keypad2",0],
99: ["keypad3",0],
100: ["keypad4",0],
101: ["keypad5",0],
102: ["keypad6",0],
103: ["keypad7",0],
104: ["keypad8",0],
105: ["keypad9",0],
106: ["keypad*",0],
107: ["keypad+",0],
108: ["No Key Assigned",0],
109: ["keypad-",0],
110: ["keypad.",0],
111: ["keypad/",0],
112: ["f1",0],
113: ["f2",0],
114: ["f3",0],
115: ["f4",0],
116: ["f5",0],
117: ["f6",0],
118: ["f7",0],
119: ["f8",0],
120: ["f9",0],
121: ["f10",0],
122: ["f11",0],
123: ["f12",0],
124: ["No Key Assigned",0],
125: ["No Key Assigned",0],
126: ["No Key Assigned",0],
127: ["No Key Assigned",0],
128: ["No Key Assigned",0],
129: ["No Key Assigned",0],
130: ["No Key Assigned",0],
131: ["No Key Assigned",0],
132: ["No Key Assigned",0],
133: ["No Key Assigned",0],
134: ["No Key Assigned",0],
135: ["No Key Assigned",0],
136: ["No Key Assigned",0],
137: ["No Key Assigned",0],
138: ["No Key Assigned",0],
139: ["No Key Assigned",0],
140: ["No Key Assigned",0],
141: ["No Key Assigned",0],
142: ["No Key Assigned",0],
143: ["No Key Assigned",0],
144: ["numlock",0],
145: ["scrolllock",0],
146: ["No Key Assigned",0],
147: ["No Key Assigned",0],
148: ["No Key Assigned",0],
149: ["No Key Assigned",0],
150: ["No Key Assigned",0],
151: ["No Key Assigned",0],
152: ["No Key Assigned",0],
153: ["No Key Assigned",0],
154: ["No Key Assigned",0],
155: ["No Key Assigned",0],
156: ["No Key Assigned",0],
157: ["No Key Assigned",0],
158: ["No Key Assigned",0],
159: ["No Key Assigned",0],
160: ["No Key Assigned",0],
161: ["No Key Assigned",0],
162: ["No Key Assigned",0],
163: ["No Key Assigned",0],
164: ["No Key Assigned",0],
165: ["No Key Assigned",0],
166: ["No Key Assigned",0],
167: ["No Key Assigned",0],
168: ["No Key Assigned",0],
169: ["No Key Assigned",0],
170: ["No Key Assigned",0],
171: ["No Key Assigned",0],
172: ["No Key Assigned",0],
173: ["No Key Assigned",0],
174: ["No Key Assigned",0],
175: ["No Key Assigned",0],
176: ["No Key Assigned",0],
177: ["No Key Assigned",0],
178: ["No Key Assigned",0],
179: ["No Key Assigned",0],
180: ["No Key Assigned",0],
181: ["No Key Assigned",0],
182: ["No Key Assigned",0],
183: ["No Key Assigned",0],
184: ["No Key Assigned",0],
185: ["No Key Assigned",0],
186: [";",0],
187: ["=",0],
188: [",",0],
189: ["-",0],
190: [".",0],
191: ["/",0],
192: ["'",0],
193: ["No Key Assigned",0],
194: ["No Key Assigned",0],
195: ["No Key Assigned",0],
196: ["No Key Assigned",0],
197: ["No Key Assigned",0],
198: ["No Key Assigned",0],
199: ["No Key Assigned",0],
200: ["No Key Assigned",0],
201: ["No Key Assigned",0],
202: ["No Key Assigned",0],
203: ["No Key Assigned",0],
204: ["No Key Assigned",0],
205: ["No Key Assigned",0],
206: ["No Key Assigned",0],
207: ["No Key Assigned",0],
208: ["No Key Assigned",0],
209: ["No Key Assigned",0],
210: ["No Key Assigned",0],
211: ["No Key Assigned",0],
212: ["No Key Assigned",0],
213: ["No Key Assigned",0],
214: ["No Key Assigned",0],
215: ["No Key Assigned",0],
216: ["No Key Assigned",0],
217: ["No Key Assigned",0],
218: ["No Key Assigned",0],
219: ["[",0],
220: ["\\",0],
221: ["]",0],
222: ["#",0],
223: ["No Key Assigned",0],
224: ["No Key Assigned",0],
225: ["No Key Assigned",0],
226: ["No Key Assigned",0],
227: ["No Key Assigned",0],
228: ["No Key Assigned",0],
229: ["No Key Assigned",0],
230: ["No Key Assigned",0],
231: ["No Key Assigned",0],
232: ["No Key Assigned",0],
233: ["`",0]
};
var KeysPressed ="";
var Counter = 0;
var ActiveKeys = function(){
document.addEventListener('keydown', function(event) {
Listener(event.keyCode,"keydown");
}, false);
document.addEventListener('keyup', function(event) {
Listener(event.keyCode,"keyup");
}, false);
document.removeEventListener('keydown',function(event){
Listener(event.keyCode,"keydown");
},false);
document.removeEventListener('keyup',function(event){
Listener(event.keyCode,"keyup");
},false);
}
var Listener = function(key,mode){
var i = 1;
var j = 1;
if(mode=="keydown"){
if(Key[key][1]==0){
console.log(Key[key][0]);
Key[key][1]=1;
}
}else if(mode=="keyup"){
if(Key[key][1]>0){
console.log(Key[key][0]);
Key[key][1]=0;
}
}
}
//Export Library
ArcKeyboard.ActiveKeys=ActiveKeys;
})(ArcKeyboard || (ArcKeyboard={}));
最佳答案
请更改以下内容:
在开始时将变量赋值给函数
var ArcKeyboard = (function(){
...
最后返回公共(public)函数
//Export Library
return {
ActiveKeys:ActiveKeys
};
})();
这里有一个工作示例:http://jsfiddle.net/6rP37/
干杯!
关于Javascript 事件处理崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20564186/
我正在尝试将 WPF CodeBehid 事件(如 Event、Handler、EventSetter)转换为 MVVM 模式。我不允许使用 System.Windows.Controls,因为我使用
我可能误解了 Backbone 中的事件系统,但是当我尝试以下代码时什么也没有发生。当我向 Backbone.Events 扩展对象添加新属性时,它不应该触发某种更改、更新或重置事件吗?就像模型一样吗
我遇到了一个简单的问题,就是无法弄清楚为什么它不起作用。我有一个子组件“app-buttons”,其中我有一个输入字段,我想听,所以我可以根据输入值过滤列表。 如果我将输入放在我有列表的根组件中,一切
System.Timers.Timer 的 Elapsed 事件实际上与 System.Windows.Forms.Timer 的 Tick 事件相同吗? 在特定情况下使用其中一种比使用另一种有优势吗
嗨,这个 javascript 代码段是什么意思。(evt) 部分是如此令人困惑.. evt 不是 bool 值。这个怎么运作? function checkIt(evt) { evt
我正在使用jquery full calendar我试图在事件被删除时保存它。 $('calendar').fullCalendar ({
我有两个链接的鼠标事件: $('body > form').on("mousedown", function(e){ //Do stuff }).on("mouseup", function(
这是我的代码: $( '#Example' ).on( "keypress", function( keyEvent ) { if ( keyEvent.which != 44 ) {
我尝试了 dragOver 事件处理程序,但它没有正常工作。 我正在研究钢琴,我希望能够弹奏音符,即使那个键上没有发生鼠标按下。 是否有事件处理程序? 下面是我正在制作的钢琴的图片。 最佳答案 您应该
当悬停在相邻文本上时,我需要使隐藏按钮可见。这是通过 onMouseEnter 和 onMouseLeave 事件完成的。但是当点击另外的文本时,我需要使按钮完全可见并停止 onMouseLeave
我有ul标签内 div标签。我申请了mouseup事件 div标记和 click事件 ul标签。 问题 每当我点击 ul标签,然后都是 mouseup和 click事件被触发。 我想要的是当我点击 u
我是 Javascript 和 jQuery 的新手,所以我有一个非常愚蠢的疑问,请耐心等待 $(document).click(function () { alert("!"); v
我有一个邮政编码解析器,我正在使用 keyup 事件处理程序来跟踪输入长度何时达到 5,然后查询服务器以解析邮政编码。但是我想防止脚本被不必要地调用,所以我想知道是否有一种方法可以跟踪 keydown
使用事件 API,我有以下代码来发布带有事件照片的事件 $facebook = new Facebook(array( "appId" => "XXX", "se
首次加载 Microsoft Word 时,既不会触发 NewDocument 事件也不会触发 DocumentOpen 事件。当 Word 实例已打开并打开新文档或现有文档时,这些事件会正常触发。
我发现了很多相关问题(这里和其他地方),但还没有具体找到这个问题。 我正在尝试监听箭头键 (37-40) 的按键事件,但是当以特定顺序使用箭头键时,后续箭头不会生成“按键”事件。 例子: http:/
给定的 HTML: 和 JavaScript 的: var $test = $('#test'); $test.on('keydown', function(event) { if (eve
我是 Node.js 的新手,希望使用流运行程序。对于其他程序,我必须同时启动一个服务器(mongodb、redis 等),但我不知道我是否应该用这个运行一个服务器。请让我知道我哪里出了问题以及如何纠
我正在尝试使用 Swift 和 Cocoa 创建一个适用于 OS X 的应用程序。我希望应用程序能够响应关键事件,而不将焦点放在文本字段上/文本字段中。我在 Xcode 中创建了一个带有 Storyb
我有以下代码: (function(w,d,s,l,i){ w[l]=w[l]||[];w[l].push({
我是一名优秀的程序员,十分优秀!