Is it possible to have keyboard event listener canvas.addEventListener('onkeydown', ev_keydown, false);
like we have Mouse event Listeners
canvas.removeEventListener('mousedown', ev_mousedown, false);
canvas.addEventListener('mousedown', ev_mousedown, false);
in JavaScript. If not then what would be the alternate?
Is it possible to have keyboard event listener canvas.addEventListener('onkeydown', ev_keydown, false);
like we have Mouse event Listeners
canvas.removeEventListener('mousedown', ev_mousedown, false);
canvas.addEventListener('mousedown', ev_mousedown, false);
in JavaScript. If not then what would be the alternate?
Share edited Oct 3, 2019 at 16:07 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Apr 7, 2012 at 18:31 Mushahid HussainMushahid Hussain 4,06511 gold badges46 silver badges64 bronze badges2 Answers
Reset to default 2Check if this works for you. Your sample line had the a prefix of on which is only used for IEs method attachEvent.
function listener(elem, evnt, func)
{
if (elem.addEventListener)
elem.addEventListener(evnt,func,false);
else if (elem.attachEvent) // For IE
return elem.attachEvent("on" + evnt, func);
}
listener(document.getElementById('myCanvas'), 'keydown', ev_keydown);
jQuery offers a simple way to bind eventlisteners to DOMElements
and there are also eventlisteners for keyboard events here are some links
http://api.jquery./keydown/
http://api.jquery./keypress/
http://api.jquery./keyup/
you can bind them to window and this should do what you want
you can also use your own method to bind events in a cross-browser patible way
function bindEvent(e, typ, handler) {
if(e.addEventListener) {
e.addEventListener(typ, handler, false);
}else{
e.attachEvent('on'+typ, handler);
}
}
this should also allow you to bind the mentioned types of events
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744931085a4601742.html
评论列表(0条)