javascript - JS - Disable Eventlistener once input field is selected -
i'm adding eventlistener document. works great using following code:
document.addeventlistener('keydown', function(event) { if(event.keycode === 68){ socket.emit('keypress', {inputid: 'right', state: true}); } else if(event.keycode === 83){ socket.emit('keypress', {inputid: 'down', state: true}); } else if(event.keycode === 65){ socket.emit('keypress', {inputid: 'left', state: true}); } else if(event.keycode === 87){ socket.emit('keypress', {inputid: 'up', state: true}); } }); document.addeventlistener('keyup', function(event) { if(event.keycode === 68){ socket.emit('keypress', {inputid: 'right', state: false}); } else if(event.keycode === 83){ socket.emit('keypress', {inputid: 'down', state: false}); } else if(event.keycode === 65){ socket.emit('keypress', {inputid: 'left', state: false}); } else if(event.keycode === 87){ socket.emit('keypress', {inputid: 'up', state: false}); } });
but once select input field , type containing w, a, s, d eventlistener triggered.
is there way disable eventlistener once input field selected?
you need disable event bubbling input. this:
input.addeventlistener('keyup', function (e) { e.stoppropagation(); }, false); input.addeventlistener('keydown', function (e) { e.stoppropagation(); }, false);
so using code above disable keyup
, keydown
event bubbling , no 1 parent node of input not hear these events
Comments
Post a Comment