Posted by Catur Nugroho | File under :

ControlSend

Sends a string of characters to a control.
ControlSend ( "title", "text", controlID, "string" [, flag] )

Parameters
  1. title : The title of the window to access.
  2. text : The text of the window to access.
  3. controlID : The control to interact with. See Controls.
  4. string : String of characters to send to the control.
  5. flag : [optional] Changes how "keys" is processed: flag = 0 (default), Text contains special characters like + to indicate SHIFT and {LEFT} to indicate left arrow. flag = 1, keys are sent raw.

Return value 
Success: Returns 1.
Failure: Returns 0 if window/control is not found.

Remark
ControlSend works in a similar way to Send but it can send key strokes directly to a window/control, rather than just to the active window.

ControlSend is only unreliable for command prompts as that works differently to normal windows (seems to check physical states rather than accepting the keystroke messages). For normal windows ControlSend should be way more reliable than a normal Send - and yes it does send shift, ctrl, alt etc.

As mention in the Send help the keyboard that send different chars when in CAPS LOCK and using the Shift Key cannot be simulated. An example is the Czech Keyboard. A good workaround is to use the ControlSetText.

The control might first need to be given focus with the ControlFocus command, specially when referencing an controlID created by the script itself.

Opt("SendKeyDelay",...) alters the length of the brief pause in between sent keystrokes.
Opt("SendKeyDownDelay",...) alters the length of time a key is held down before being released during a keystroke.

 Source : Auotit Help Documents

This is the code in c# how autoit control send worked with little modification to getting perfect result.

private void Type(string strKeys, int nMode, string strControl, bool blnSleepKeys, int nSleep, string strWindow)
        {
            if (blnSleepKeys)
            {
                char[] achKeys = strKeys.ToCharArray();

                foreach (char ch in achKeys)
                {
                    _AutoIT.ControlSend(strWindow, "", strControl, ch.ToString(), nMode);
                    Thread.Sleep(100);
                    Trace.WriteLine("Sent Key: '" + ch.ToString() + "'");
                }
            }
            else
            {
                _AutoIT.ControlSend(strWindow, "", strControl, strKeys, nMode);
                Trace.WriteLine("Sent Keys: " + strKeys);
            }

            for (int i = 0; i < (nSleep / 100); i++)
            {
                Application.DoEvents();
                Thread.Sleep(100);
            }
        }

How it work? 

You can try to call notepad and send some key.
Gathering window information :
Drag Finder Tool and drop into notepad text area.



Parameters required :
Click Control Tab (Parameters required in there)
Tittle : Untitled - Notepad or Title : [CLASS:Notepad]
Text : Optional
Control : Edit1

Source Code C#
private void btnSend_Click(object sender, EventArgs e)
        {
            //run notepad.exe program
            autoit.Run("notepad.exe", "", 1);
            Thread.Sleep(1000);

            Type("autoitsourcecode.blogspot.com", 0, "Edit1", true, 2000, "[CLASS:Notepad]");
        }

with send key ENTER
private void btnSendEnter_Click(object sender, EventArgs e)
        {
            //run notepad.exe program
            autoit.Run("notepad.exe", "", 1);
            Thread.Sleep(1000);

            for (int x = 1; x < 5; x++)

            {
                //send keys
                Type("autoitsourcecode.blogspot.com", 0, "Edit1", true, 2000, "[CLASS:Notepad]");

                //send Enter

                Type("{ENTER}", 0, "Edit1", false, 2000, "[CLASS:Notepad]");
            }
           
        }

Download Full Source Code

4 comments:

  1. Could you please add Clipput() and Clipget() to AutoitX3Lib? I need them for faster copy paste
    At the moment I'm getting AutoitX3Lib.AutoitX3 does not contain a definition for Clipput....
    thank you

    ReplyDelete
  2. also CTRL+c is not supported, i guess no key combination is supported
    _autoit.ControlSend("xxx", "", "", "^{c}");

    ReplyDelete
    Replies
    1. Hi, autoit.ControlSend() is working.

      CTRL + a, CTRL + c, CTRL + v

      Also, you can see the code at here : http://www.4shared.com/zip/sweJRRWf/autoit_demo_notepad.html

      Delete