在jQuery中触发多个按键并按住事件的方法 [英] Way to trigger multiple keypress and hold events in jQuery
本文介绍了在jQuery中触发多个按键并按住事件的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
寻找一种使用 jQuery 模拟多个按键的方法.例如 ALT+F1(按住 alt 并按 F1).
Looking for a way to simulate a multiple keypress using jQuery. For example ALT+F1 (holding down alt and hitting F1).
由于以下原因,我设法模拟了 F1 按键:使用jQuery触发按键事件的确定方式
I've managed to simulate the F1 keypress thanks to: Definitive way to trigger keypress events with jQuery
寻找这样的东西:
$(".f1").click(function() {
var e = jQuery.Event("keydown");
e.keyCode = 18; // ALT key pressed
// while ALT key is pressed, hit the F1 Key
$("input").trigger(e, function(){
var e = jQuery.Event("keydown");
e.which = 112; // F1 key
$("input").trigger(e);
});
});
推荐答案
使用修饰键 alt
、ctrl
和 shift
,您可以专门为他们使用事件修饰符:
With the modifier keys alt
, ctrl
, and shift
, you can use event modifiers specifically for them:
$(".f1").click(function() {
var e = jQuery.Event("keydown");
e.which = 112; // # F1 code value
e.altKey = true; // Alt key pressed
$("input").trigger(e);
});
演示:http://jsfiddle.net/jtbowden/2kcrg/
这篇关于在jQuery中触发多个按键并按住事件的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文