我在这里看到一些类似的问题(比如 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/whatever,所以这些解决方案是不可接受的。
那么…我怎样才能检测到 ctrl
?
原文由 zozo 发布,翻译遵循 CC BY-SA 4.0 许可协议
尝试使用
if (e.ctrlKey)
。MDN: 事件.ctrlKey