27

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

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

diewland
  • 1,865
  • 5
  • 30
  • 41
  • 1
    That would be a massively big security vulnerability, and I wouldn't recommend going down the ActiveX path - it'll prompt users and only work in IE. The other option is if you make an application that hosts a WebBrowser control. Seems a bit dubious, what is it for? – Jeremy Thompson May 13 '11 at 07:04
  • For tabIndex purpose. If I can do this, I don't need to implement next tabIndex element logic in event handling. – diewland May 13 '11 at 07:57
  • you mean the tabindex attribute of form elements? http://www.htmlcodetutorial.com/forms/_INPUT_TABINDEX.html Why do you need to send keystrokes for that? – rdmueller May 13 '11 at 09:03
  • I will do function that jump to next textfield when text input equals textfield maxlength. So if I use tabIndex, I must looking for next element by myself. But If I can simulate tab key, Event is simple. Just fire press tab key and use html tabindex mechanism for that. – diewland May 13 '11 at 10:08
  • For your purpose you can evaluate onkeyup the length of the input in which you are typing in (the focussed input text box). Then, if the typed text length condition is verified you may use something like `this.nextSibling.focus();` – willy wonka Feb 06 '17 at 19:33

4 Answers4

10

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.

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.

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

zeacuss
  • 2,563
  • 2
  • 28
  • 32
rdmueller
  • 10,742
  • 10
  • 69
  • 126
  • You CAN send keystrokes on the OS Level. – fjf2002 Feb 27 '19 at 15:03
  • @fjf2002 can you present an exampel for instance in jsfiddle.net? – rdmueller Feb 27 '19 at 19:24
  • I hope I'm not being misunderstood. I wanted to say SendKeys is available in VBA or C# or Powershell. But not in the Javascript Sandbox, of course. – fjf2002 Feb 27 '19 at 19:32
  • ah, yes. Sorry, I did understand you wrong, but this is now clarified. You can also send keystrokes with javascript, but I guess not in a sandboxed browser, only from "trusted" javascript – rdmueller Feb 27 '19 at 19:37
4

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

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

Javascript: simulate a click on a link for YUI

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

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

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

Create a loop to generate the list when document is loaded (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.

Community
  • 1
  • 1
tytsim
  • 139
  • 7
1

This works for me. The ActiveXObject needs to be opened in 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);
                        }
                    }
0

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.

icktoofay
  • 126,289
  • 21
  • 250
  • 231
  • These days browser specific solutions are not required. There are too many good JS libraries that'll do what you need. – Maladon Sep 05 '13 at 19:06
  • 2
    @Maladon: If there's a browser-agnostic JavaScript library that would allow sending keystrokes to be interpreted by the operating system, I'd be very worried. – icktoofay Sep 06 '13 at 02:32