简体   繁体   English

jQuery 按键事件没有发生?

[英]jQuery Keypress event doesn't occur?

I want to build a small script which triggers the jump move in a Windows fangame.我想构建一个小脚本来触发 Windows fangame 中的跳跃动作。 In the game, the jump key is "Z" (KeyCode 90 in my case).在游戏中,跳跃键是“Z”(在我的例子中是 KeyCode 90)。 So I built this script:所以我构建了这个脚本:

var press = jQuery.Event("keypress");
press.ctrlKey = false;
press.which = 90;
setInterval(function() { $("whatever").trigger(press) }, 2000 );

But when I execute it, nothing happens (I tested it with Firefox).但是当我执行它时,什么也没有发生(我用 Firefox 测试过)。 I added console.log("Done") to the function just to see if it works, and it did.我将console.log("Done")添加到 function 只是为了查看它是否有效,并且确实有效。 I also tried using a textbox seeing if it would maybe write the letters there, but it didn't as well.我还尝试使用文本框查看它是否可以在那里写字母,但它也没有。 It's like the event gets ignored by JS.就像事件被 JS 忽略一样。

Can I do something like that in the Browser?我可以在浏览器中做类似的事情吗? Or is it impossible to do so?还是不可能这样做?

Thanks in advance提前致谢

I want to build a small script which triggers the jump move in a Windows fangame.我想构建一个小脚本来触发 Windows fangame 中的跳跃动作。 In the game, the jump key is "Z" (KeyCode 90 in my case).在游戏中,跳跃键是“Z”(在我的例子中是 KeyCode 90)。 So I built this script:所以我构建了这个脚本:

var press = jQuery.Event("keypress");
press.ctrlKey = false;
press.which = 90;
setInterval(function() { $("whatever").trigger(press) }, 2000 );

But when I execute it, nothing happens (I tested it with Firefox).但是当我执行它时,什么也没有发生(我用 Firefox 测试过)。 I added console.log("Done") to the function just to see if it works, and it did.我将console.log("Done")添加到 function 只是为了查看它是否有效,并且确实有效。 I also tried using a textbox seeing if it would maybe write the letters there, but it didn't as well.我还尝试使用文本框查看它是否可以在那里写字母,但它也没有。 It's like the event gets ignored by JS.就像事件被 JS 忽略一样。

Can I do something like that in the Browser?我可以在浏览器中做类似的事情吗? Or is it impossible to do so?还是不可能这样做?

Thanks in advance提前致谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM