gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 04:45:28 25 4
gpt4 key购买 nike

我在这里看到一些类似的问题(例如 JavaScript: Check if CTRL button was pressed ),但我的问题实际上是事件触发。我的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/原型(prototype)/任何东西,所以这些解决方案是 Not Acceptable 。

那么...我如何检测ctrl

最佳答案

尝试使用if (e.ctrlKey)

MDN: event.ctrlKey

关于javascript - 检测按下或向上按下 ctrl 键,按键事件不会触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43415476/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com