简体   繁体   English

按键不起作用Javascript

[英]Key Up isn't working Javascript

I have no idea how this works. 我不知道这是如何工作的。 I am trying to learn key events. 我正在尝试学习关键事件。 I got keydown to work, but key up just won't work. 我需要按下按键才能工作,但是按下按键就无法工作。 Any help appreciated. 任何帮助表示赞赏。

      window.onload = function(){

        window.addEventListener('keyup' , loopit1 ); // this doesn't work.
        /*window.addEventListener('keydown' , loopit2 );*/ // this works. That's why it's commented out.

        function loopit1(e){
        if(e.ctrlKey){
        alert("Key Up!");
        }
        //This doesn't work.
        }

        /*function loopit2(e){
        if(e.ctrlKey){
        alert("Key Down!");
        }
        }*// This Works. That's why it's commented out.

}

It does work. 确实有效。 The ctrlKey property indicates whether the Ctrl key is pressed at the time that the event fires. ctrlKey属性指示事件触发时是否按下Ctrl键。 Therefore, it won't fire if the Ctrl key was the key that just came up, since the keyup event fires temporally after the Ctrl key comes up. 因此,如果Ctrl键只是刚出现的键,则不会触发,因为keyup事件会在Ctrl键出现后临时触发。 If the Ctrl key is down while another key comes up, then e.ctrlKey will be true. 如果在Ctrl 另一个键的同时Ctrl键,则e.ctrlKey为true。

However, it needs to be a key that doesn't cause a browser action that takes away focus from the document -- if I bring the T key up while holding down Ctrl , the browser will move to a new tab before the event can happen. 但是,它必须是一个不会导致浏览器操作脱离文档焦点的键-如果在按住Ctrl同时调出T键,浏览器将移至新标签页,然后再发生事件。 Try out Ctrl+Alt : while holding down the Ctrl key, press and release Alt . 尝试Ctrl+Alt :在按住Ctrl键的同时,按下并释放Alt

If you want to test whether the Ctrl key itself has been lifted, you should check e.keyCode for the value 17 . 如果要测试Ctrl键本身是否已抬起,则应检查e.keyCode的值17 However, e.keyCode is deprecated , and should be replaced with e.key in the future. 但是, e.keyCode已弃用 ,以后应将其替换为e.key However, e.key is not yet widely implemented, so you must use keyCode for the time being. 但是, e.key尚未得到广泛实施,因此您必须暂时使用keyCode

if you log out the event object, you'll see that e.ctrlKey = true on the keydown event, but e.ctrlKey = false on the keyup event. 如果注销事件对象,则会在keydown事件上看到e.ctrlKey = true ,但在keyup事件上看到e.ctrlKey = false

Why you ask? 你为什么问?

I can't really refer you to a source or any facts, but my thesis is like this: 我无法真正向您推荐任何来源或任何事实,但是我的论文是这样的:

The e.ctrlKey property is there for you to check if the ctrl key is pressed down while you click a button. 您可以在其中使用e.ctrlKey属性来检查单击按钮时是否按下了ctrl键。 As you'll find out by checking it out, the event object has a lot of these properties for you to check. 正如您将通过检出发现的那样,事件对象具有许多这些属性供您检查。 This way, it's easier to check if the user is clicking ctrl+s or alt+f7 or whatever. 这样,检查用户是否单击ctrl + s或alt + f7或其他按钮会更容易。

The reason the e.ctrlKey always is false is probably because is never needed. e.ctrlKey始终为false的原因可能是因为不需要。 Weird perhaps, but it doesn't really make sense to have it set in the first place, given my argument above. 也许很奇怪,但是考虑到我上面的论点,将其设置在第一位并没有任何意义。

A solution to your problem is to check the keyCode property instead - the key for ctrl is 17. 解决您的问题的方法是改为检查keyCode属性-ctrl的键为17。

function loopit1(e){
    if(e.keyCode === 17){
        alert("Key Up!");
    }
}

Hope this helps! 希望这可以帮助!

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

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