简体   繁体   English

JS - 键盘事件 - 听/不听

[英]JS - keyboard events - listen / unlisten

I have an issue concerning the keyboards events in JS. 我有一个关于JS中键盘事件的问题。

First of all, please do not answer me to use jQuery methods , I know most of it (bind/unbind, on/off, one...) but I work with an internal framework that have to work without jQuery , even if jQuery is used on most of our projects. 首先, 请不要回答我使用jQuery方法 ,我知道它的大部分内容(bind / unbind,on / off,one ...)但我使用的内部框架必须在没有jQuery的情况下工作 ,即使jQuery也是如此用于我们的大多数项目。

So, I have a module, in fact a swipe tool based on Swipe.js and extended to work on web and mobile environments (compatibility needed for IE 7+, WebKit (Chrome & Safari), Moz, Opera and IE10 / Windows Phone) 所以,我有一个模块,实际上是一个基于Swipe.js的滑动工具,可以扩展到在Web和移动环境中工作(IE 7 +,WebKit(Chrome和Safari),Moz,Opera和IE10 / Windows Phone需要兼容)

In do not have any problem with mouse/touch events, the binding and unbinding methods inspired from the mobile HTML5 BP seems to work very well with a small correction for the detachEvents method. 在鼠标/触摸事件没有任何问题的情况下,灵感来自移动HTML5 BP的绑定和解除绑定方法似乎可以很好地处理detachEvents方法的小修正。

And then I would to implement a keyboard control feature based on 'keydown' events. 然后我将基于'keydown'事件实现键盘控制功能。 (BTW, I am not sure to make a good difference between keydon and keypress events, except the keypressEvent.preventDefault() do not prevent the small scroll effect, so I use keydown.) (顺便说一句,我不确定keydon和keypress事件之间有什么区别,除了keypressEvent.preventDefault()不会阻止小滚动效果,所以我使用keydown。)

As it is possible to add many Swipes on the same page, I start to listen the keydown events only when any Swipe is focused (Note that I add a "tabindex" attribute to allow the element to get focused). 由于可以在同一页面上添加许多Swipe,因此只有在任何Swipe聚焦时才会开始监听keydown事件(请注意,我添加了一个“tabindex”属性以允许元素聚焦)。

<div id="swipe1" class="swipe" tabindex='0'>
    <ul>
        [...]
    </ul>
</div>

Then when the Swipe handle a 'touchstart' / 'click' / ' MSPointerDown' event, I focus it : 然后当Swipe处理'touchstart'/'click'/'MSPointerDown'事件时,我会关注它:

onTouchStart: function(e) {
    this.container.focus(); // Refers to the div#swipe1.swipe element
    [...]
    return false;
}

onFocus: function (e) {
    if (this.activateKeyboardControls) { // Keyboard control is optional
        this.addListener(document, 'keydown', this, true);
    }
}

onBlur: function (e) {
    if (this.activateKeyboardControls) { // Keyboard control is optional
        this.removeListener(document, 'keydown', this, true);
    }
}

But unfortunately, the removeListener does not work. 但不幸的是,removeListener不起作用。

I mean, when the element loses the focus (blur event fired), it still handle the keydown events... 我的意思是,当元素失去焦点(模糊事件被触发)时,它仍然处理keydown事件......

Is it because it is binded on the document object ? 是因为它在文档对象上绑定了吗? I have read some solutions working with some booleans but I am looking for a cleaner way the manage it. 我已经阅读了一些使用一些布尔值的解决方案,但我正在寻找一种更清洁的方式来管理它。

This is annoying, because when I give the focus to many Swipes, each of them is swipping when I press the keyboard. 这很烦人,因为当我把焦点放在很多Swipes上时,当我按下键盘时,每个Swipe都会滑动。

Thanks in advance ! 提前致谢 !

I'm not sure of the reason why my answer has been deleted - more than 2 weeks after posting - but anyway, this is how I solved it : 我不确定我的答案被删除的原因 - 发布后超过2周 - 但无论如何,这就是我解决它的方式:

It comes from the 'type' parameter of the addEventListener / attachEvent method, the first of one so... 它来自addEventListener / attachEvent方法的'type'参数,这是第一个...

I bind it on the object instead of on the window, and without bubbling. 我将它绑定在对象而不是窗口上,并且没有冒泡。

var target = e.target || e.srcElement;
if (this.activateKeyboardControls) {
    this.addListener(target, 'keydown', this, false);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM