如何在 JavaScript 中模拟按键?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35143695/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 17:19:00  来源:igfitidea点击:

How can I simulate a keypress in JavaScript?

javascriptjqueryevents

提问by jsv

I'm trying to find a way to simulate a keypress.

我正在尝试找到一种模拟按键的方法。

For example, when function launched, the key "Arrow Down" should be pressed and so the webpage should be slightly scrolled.

例如,当功能启动时,应按下“向下箭头”键,因此应稍微滚动网页。

I'm interested only in Chrome, and both jQuery or plain JS will be appropriate. (Plain JS will be more preferable).

我只对 Chrome 感兴趣,jQuery 或普通 JS 都适合。(纯 JS 会更可取)。

That's one of the code examples I tried:

这是我尝试过的代码示例之一:

var e = $.Event("keydown", { keyCode: 40}); // 40 = down arrow
$("body").trigger(e);
// When I launch it the console, nothing happens. The page is not scrolled.
// May be I missed some obvious?

I searched and found the following related questions, but the solutions did not work for me:

我搜索并发现了以下相关问题,但解决方案对我不起作用:



In other words

换句话说

Using AutoHotkey, you can easily make something like:

使用 AutoHotkey,您可以轻松制作以下内容:

Down::
Send, {Up}

Then, if you press Downarrow key, it will be triggered Up. I just want to implement it with JS.

然后,如果您按Down箭头键,它将被触发Up。我只想用JS实现它。

采纳答案by Gavriel

As @rfsbsb pointed out from: Jquery script for simulated key press down not running keyboard shortcut

正如@rfsbsb 指出的那样:用于模拟按键按下的 Jquery 脚本不运行键盘快捷键

If you're trying to fire some browser or system wide keyboard shortcut then it's a dead end - it can't be done for security reasons. If it would be possible, you would have pages all over the Internet that would (for example) add themself to your bookmarks without even asking (by firing CTRL+B shortcut using Javascript).

如果您试图触发某些浏览器或系统范围的键盘快捷键,那么这是一个死胡同 - 出于安全原因无法这样做。如果可能的话,您会在整个 Internet 上拥有一些页面,这些页面会(例如)将自己添加到您的书签中,甚至无需询问(通过使用 Javascript 触发 CTRL+B 快捷方式)。

回答by Haring10

Using this answer, I managed to change the code a bit and I think I got what you are looking for?

使用这个答案,我设法稍微改变了代码,我想我得到了你想要的东西?

here is my jsfiddle

这是我的 jsfiddle

Code:

代码:

jQuery(document).ready(function($) {
    $('body').keypress(function(e) {
        if(e.which == '40') 
            $('body').animate({scrollTop: '100px'});
    });
});
jQuery.fn.simulateKeyPress = function(character) {
    jQuery(this).trigger({
        type: 'keypress',
        which: character
    });
};

 setTimeout(function() {
    $('body').simulateKeyPress(40);
 }, 1000);

回答by Doru P?rvu

Here is a sample starting from @Haring10's example:

这是从@Haring10 的示例开始的示例:

https://jsfiddle.net/po33vfb4/4/

https://jsfiddle.net/po33vfb4/4/

$('body').keydown(function(e) {
  e.preventDefault();
  e.stopImmediatePropagation();

  if(e.which == 40) {
    window.scrollBy(0, -100);
  } else {
    window.scrollBy(0, 100);
  }
});

Triggering keydown-keyup-keypress programatically does not seem to have the efect of scrolling. The above sample can be customized to add animations.

以编程方式触发 keydown-keyup-keypress 似乎没有滚动的效果。上面的示例可以自定义添加动画。