Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I do SendKeys in Javascript?

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

like image 656
diewland Avatar asked May 13 '11 06:05

diewland


2 Answers

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

like image 198
rdmueller Avatar answered Sep 21 '22 20:09

rdmueller


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.

like image 33
tytsim Avatar answered Sep 21 '22 20:09

tytsim