我可以在 Javascript 中执行 SendKeys 吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5988230/
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 19:46:34  来源:igfitidea点击:

Can I do SendKeys in Javascript?

javascriptsendkeys

提问by diewland

SendKeys is method to sending keystroke to an application.
Can I do it in Javascript, to send keystroke in browser ?

SendKeys 是将击键发送到应用程序的方法。
我可以用 Javascript 来做,在浏览器中发送击键吗?

REF :
http://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.aspx

参考:http :
//msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.aspx

采纳答案by rdmueller

If you would be able to send keystrokes on the OS Level, this would be a big security issue. You could (for instance) install any kind of software on the client machine if you where able to send keystrokes to the needed install dialogs.

如果您能够在操作系统级别发送击键,这将是一个很大的安全问题。如果您能够将击键发送到所需的安装对话框,您可以(例如)在客户端计算机上安装任何类型的软件。

Yes, you could come up with an active-x control or some other tools to be installed on the client machine. But because of the security issues with such a tool, I wouldn't do it -- even in a controlled environment.

是的,您可以想出一个 active-x 控件或一些其他工具来安装在客户端计算机上。但是由于这种工具的安全问题,我不会这样做——即使是在受控环境中。

Most of the time, there is a way to achieve your needed functionality without breaching the security.

大多数情况下,有一种方法可以在不破坏安全性的情况下实现所需的功能。

Update:If you want to jump to the next tabfield, you have to use the focus() method to set the focus to the next element. Unfortunately, you have to find the next element by yourself in javascript, but this should not be a big problem - you can keep an ordered list of all your elements in javascript.

更新:如果你想跳转到下一个 tabfield,你必须使用 focus() 方法将焦点设置到下一个元素。不幸的是,您必须自己在 javascript 中找到下一个元素,但这应该不是什么大问题 - 您可以在 javascript 中保留所有元素的有序列表。

btw: http://forums.devarticles.com/javascript-development-22/moving-to-next-tabindex-on-event-2382.html

顺便说一句:http: //forums.devarticles.com/javascript-development-22/moving-to-next-tabindex-on-event-2382.html

回答by tytsim

There are lots of JS Framework implemented event simulation inside web page.

有很多 JS 框架在网页内部实现了事件模拟。

Is it possible to simulate key press events programmatically?for jQuery

是否可以以编程方式模拟按键事件?对于 jQuery

Javascript: simulate a click on a linkfor YUI

Javascript:模拟点击YUI的链接

However, simpler method is that the third post of the link given by Ralfwhich focus the "next" textfield regarding to the tabIndex property of elements inside a form element.

然而,更简单的方法是Ralf给出的链接的第三篇文章,它关注与表单元素内元素的 tabIndex 属性有关的“下一个”文本字段。

There might be a more brilliant way if you make up a list of textfield's IDs and the order you want to be.

如果您组成一个文本字段 ID 列表和您想要的顺序,可能会有更出色的方法。

Of course, the tabIndex list might not be generated by yourself but by walking around the textfield.

当然,tabIndex 列表可能不是您自己生成的,而是通过在文本字段中走动生成的。

Create a loop to generate the list when document is loaded (DOMContentLoaded):

创建一个循环以在加载文档时生成列表 (DOMContentLoaded):

var tabIndexList = new Array();
function tabIndexListGeneration(){
   var form = document.getElementById("Your form ID"), // remember to fill in your form ID
       textfields = form.getElementsByTagName("input"), 
       textfieldsLength = textfields.length;
   for(var i=0;i<textfieldsLength;i++){
      if(textfields[i].getAttribute("type") !== "text" || textfields[i].getAttribute("tabIndex") <= 0)continue;
      /* tabIndex = 0 is neglected as it places the latest, if you want it, change 0 to -1
       *  and change tabIndexPointer = 0 into  tabIndexPointer = -1 below */
      tabIndexList[textfields[i].getAttribute("tabIndex")] = textfields[i];
   }
}
// You can use the function of JS Framework if you don't like the method below.
if(document.addEventListener){
   document.addEventListener("DOMContentLoaded", tabIndexListGeneration, false);
}else{
   window.attachEvent("onload", tabIndexListGeneration);
}

And inside the event of "text input equals textfield maxlength":

在“文本输入等于文本字段最大长度”的事件中:

var tabIndexPointer = target.getAttribute("tabIndex"); // target is the DOM object of current textfield 
while(!(++tabIndexPointer in tabIndexList)){
   if(tabIndexPointer >= tabIndexList.length)
      tabIndexPointer = 0; // or other action after all textfields were focused
}
tabIndexList[tabIndexPointer].focus();    // if other action needed, put it right after while ended

Note:form textfields' structure must not be mutated otherwise an error would be given out.

注意:表单文本字段的结构不能改变,否则会报错。

If textfield generate dynamically, run tabIndexListGeneration()to regenerate the list.

如果文本字段是动态生成的,则运行tabIndexListGeneration()以重新生成列表。

回答by user6178986

This works for me. The ActiveXObject needs to be opened in IE.

这对我有用。ActiveXObject 需要在 IE 中打开。

                var PaintProg = new ActiveXObject("WScript.Shell");     //paste to mspaint
                PaintProg.Run("mspaint.exe \\srv4\photos\image1.jpg",9,false);

                var PaintTimer = window.setInterval(PaintPaste,1000);

                function PaintPaste()
                    {
                    if (PaintProg.AppActivate("Paint",true) == true)
                        {
                        PaintProg.SendKeys('"^(v)""%(F)""x""~"',true);
                        window.clearInterval(PaintTimer);
                        }
                    }

回答by icktoofay

Not by default in most browsers, no. You may be able to get it to work using ActiveX if it's going to be running in Internet Explorer, however.

在大多数浏览器中不是默认的,不是。但是,如果它要在 Internet Explorer 中运行,则可以使用 ActiveX 使其工作。