如何识别以下代码中按下的Ctrl/Shift/Alt键?
$("#my_id").click(function() {
if (<left control key is pressed>) { alert("Left Ctrl"); }
if (<right shift and left alt keys are pressed>) { alert("Right Shift + Left Alt"); }
});
最佳答案
好吧,这不适用于所有浏览器,只有 IE 8。微软实现了确定按下哪个(右/左)键的功能。这是一个链接http://msdn.microsoft.com/en-us/library/ms534630(VS.85).aspx
我还发现了这篇关于浏览器中的 keypress、keyup、keydown 事件的精彩文章。 http://unixpapa.com/js/key.html
$('#someelement').bind('click', function(event){
if(event.ctrlKey) {
if (event.ctrlLeft) {
console.log('ctrl-left');
}
else {
console.log('ctrl-right');
}
}
if(event.altKey) {
if (event.altLeft) {
console.log('alt-left');
}
else {
console.log('alt-right');
}
}
if(event.shiftKey) {
if (event.shiftLeft) {
console.log('shift-left');
}
else
{
console.log('shift-right');
}
}
});
关于javascript - 检查 'click' 事件上的 Ctrl/Shift/Alt 键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34102228/