javascript - 按下或按下ctrl键,按键事件不会触发 [英] javascript - detect ctrl key pressed or up, keypress event doesn't trigger

查看:612
本文介绍了javascript - 按下或按下ctrl键,按键事件不会触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里看到一些类似的问题(例如 JavaScript:检查CTRL按钮是否为按下)但我的问题实际上是事件触发。我的js代码:

I see some similar questions here (like JavaScript: Check if CTRL button was pressed) but my problem is actually the event triggering. My js code:

    // Listen to keyboard. 
    window.onkeypress = listenToTheKey;
    window.onkeyup = listenToKeyUp;

    /*
        Gets the key pressed and send a request to the associated function
        @input key
    */
    function listenToTheKey(e)
    {
        if (editFlag == 0)
        {
            // If delete key is pressed calls delete
            if (e.keyCode == 46)
                deleteNode();

            // If insert key is pressed calls add blank
            if (e.keyCode == 45)
                createBlank();

            if (e.keyCode == 17)
                ctrlFlag = 1;
        }
    }

ctrl 。

我还需要为 ctrl 触发它。

我不能使用jQuery / prototype /无论如何那些解决方案是不可接受的。

The event triggers for any other keys except the ctrl.
I need to also trigger it for ctrl.
I can't use jQuery/prototype/whatever so those solutions are not acceptable.

那么......我怎样才能检测到 ctrl

So... how can I detect the ctrl?

推荐答案

尝试使用 if(e.ctrlKey)

< a href =https://developer.mozilla.org/en-US/docs/Web/API/event.ctrlKey\"rel =noreferrer> MDN:event.ctrlKey

这篇关于javascript - 按下或按下ctrl键,按键事件不会触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆