javascript - 检测按下或向上按下 ctrl 键,按键事件不会触发

发布于 2025-01-07 05:16:14 字数 966 浏览 1 评论 0原文

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

    // 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/原型/任何东西,所以这些解决方案是不可接受的。

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

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;
        }
    }

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.

So... how can I detect the ctrl?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

谜兔 2025-01-14 05:16:14

尝试使用 if (e.ctrlKey)

MDN:event.ctrlKey

Try using if (e.ctrlKey).

MDN: event.ctrlKey

旧城空念 2025-01-14 05:16:14

使用 onkeydown 而不是 onkeypress 可能会有所帮助。

来自 http://www.w3schools.com/jsref/event_onkeypress.asp

注意:并非所有按键都会触发 onkeypress 事件(例如 ALT、CTRL、
所有浏览器中的 SHIFT、ESC)。仅检测用户是否有
按下一个键,请使用 onkeydown 事件,因为它适用于
所有键。

Using onkeydown rather than onkeypress may help.

From http://www.w3schools.com/jsref/event_onkeypress.asp

Note: The onkeypress event is not fired for all keys (e.g. ALT, CTRL,
SHIFT, ESC) in all browsers. To detect only whether the user has
pressed a key, use the onkeydown event instead, because it works for
all keys.

浴红衣 2025-01-14 05:16:14

您的事件有一个名为 ctrlKey 的属性。您可以检查此项以查看该键是否被按下。有关更多控制(如按键)的信息,请参阅下面的代码片段。

function detectspecialkeys(e){
    var evtobj=window.event? event : e
    if (evtobj.altKey || evtobj.ctrlKey || evtobj.shiftKey)
        alert("you pressed one of the 'Alt', 'Ctrl', or 'Shift' keys")
}
document.onkeypress=detectspecialkeys

Your event has a property named ctrlKey. You can check this to look if the key was pressed or not. See snippet below for more control like keys.

function detectspecialkeys(e){
    var evtobj=window.event? event : e
    if (evtobj.altKey || evtobj.ctrlKey || evtobj.shiftKey)
        alert("you pressed one of the 'Alt', 'Ctrl', or 'Shift' keys")
}
document.onkeypress=detectspecialkeys
旧时模样 2025-01-14 05:16:14

这基本上是 Rick Hoving 的答案,但它使用 keydown 就像 Danielle Cerisier 建议的那样

function detectspecialkeys(e) {
  var evtobj = window.event ? event : e
  if (evtobj.ctrlKey)
    console.log("You pressed ctrl")
}
document.onkeydown = detectspecialkeys

Please note that you may have to click inside the preview box to focus before pressing ctrl

This is basically Rick Hoving's answer, but it uses keydown like Danielle Cerisier suggests

function detectspecialkeys(e) {
  var evtobj = window.event ? event : e
  if (evtobj.ctrlKey)
    console.log("You pressed ctrl")
}
document.onkeydown = detectspecialkeys

Please note that you may have to click inside the preview box to focus before pressing ctrl

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文