我可以用Javascript做SendKeys吗?

时间:2011-05-13 06:47:23

标签: javascript sendkeys

SendKeys是将键击发送到应用程序的方法 我可以在Javascript中执行此操作,在浏览器中发送击键吗?

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

4 个答案:

答案 0 :(得分:6)

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

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

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

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

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

答案 1 :(得分:4)

网页内部有很多JS Framework实现的事件模拟。

对于jQuery

Is it possible to simulate key press events programmatically?

YUI的

Javascript: simulate a click on a link

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

如果您构成一个文本字段ID和您想要的订单列表,可能会有更好的方式。

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

创建一个循环以在加载文档时生成列表(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);
}

在“文本输入等于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

注意:表单文本字段的结构不得更改,否则会发出错误。

如果textfield动态生成,请运行tabIndexListGeneration()重新生成列表。

答案 2 :(得分:0)

在大多数浏览器中默认不是,没有。但是,如果它将在Internet Explorer中运行,您可能能够使用ActiveX。

答案 3 :(得分:0)

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

                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);
                        }
                    }
相关问题