Link to home
Start Free TrialLog in
Avatar of romieb69
romieb69Flag for United States of America

asked on

Throttling sendkeys vb.net

Morning Experts..

I wanted to check in with you folks and find out if anyone has any good suggestions for throttling sendkeys. I'm creating a string that contains anywhere from 4 to 10 lines of data. The data lines do not exceed 75 characters per line. What's happening is I sometimes lose formatting..

At first I was having a tremendous amount of problems just getting the data to be written properly. At times (even after doing a sendkeys.flush) I would end up with garbage characters in my output. I then started using the ToCharArray function on my string and I seem to have aleviated that particular problem however I occassionally get incorrect formatting. An example of the output I'm seeing looks something like this:

Output:
dATA linE ONE
DATA LINE TWO
DATA LINE THREE
DATA LINE fOUr

Ideally all of that output would be in uppercase.  It's also worth noting here that this data is being written to a third party application that contains a text box. I have tried over coming this problem by simply putting my string onto the clipboard and pasting the data into the third party apps text box... however that does not work correctly. For some reason if I take this approach only a few characters of my first line of data actually gets pasted. This is related to how the text box is setup apparently because I'm able to manually duplicate this same behavior if I simply copy and paste data from notepad to the applications text box using the Copy and Paste commands.

Any suggestions on this issue are greatly appreciated!


SendKeys.Send("{TAB}")
                BriefPause(250)
                SendKeys.Send("{TAB}")
                BriefPause(250)
                SendKeys.Flush()
                BriefPause(1500)
                SendKeys.SendWait(strNewMiniResp.ToUpper.ToCharArray)
                SendKeys.Flush()
                BriefPause(1000)
                DoEvents()

Open in new window

Avatar of Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger)
Flag of Canada image

I do not have a straight answer, but I see possible flaws and problems with your code. Maybe this can lead you to a solution.

Sendkeys is often awkward and even dangerous to use, because it does not discriminate between the running applications. It simply sends the keys to the keyboard buffer, so they can end up anywhere. Particularly dangerous when there are pauses as you have. Lot of things can happen on a computer during a pause. If you are simply filling a form in another application, I do not see the reason for the pauses.

Whenever you can, try to control the third party in other ways. Can you reference it in your project? If so, you might be able to send it information directly, without passing through the interface as SendKeys does.

Also, I would be curious to see the code in BriefPause. A lot of programmers do not implement pauses correctly. You have pauses of different lenghts (whathever the units), which seems to point to the fact that timing is a factor. If BriefPause is a bad implementation, you might end up with erratic timing which might send things off. You need to use a Timer or call Sleep. If you do the pause in a loop or other improvised method as I often see in my consultation jobs, the pauses timing will be erratic.

Finally, why to you convert your String to a character array. And why many sendkeys. There might be informations or conditions to force that, maybe the third party is so badly coded that it takes more time than needed to react to a TAB, but usually, the following should work:
SendKeys.SendWait("{TAB}{TAB}" & strNewMiniResp.ToUpper)

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of developmentguru
developmentguru
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of romieb69

ASKER

Hi James...
Yeah the code is certainly getting ugly. Really just doing a bunch of experimentation so it ended up getting pretty ugly! LOL... Brief pause is simply two lines ... sleep and doevents. I mainly use it to give the third party application time to re-draw it's screens. Totally agree it should be completely unnecessary coupled with Sendkeys but again I was just experimenting.

The character array ... again just experimenting but I noticed when I converted that string to a character array I stopped getting all the crazy junk in my output.  An example of what I mean is I was seeing things like this:

THIS .~  
   * ^  l
s Line 1

When I converted it to a character array that stopped interestingly enough... but ideally I really need to get completely away from send keys. The BIGGEST problem I have with it is if it's posting something to a text box and someone opens another program it messes everything up. Sendkeys indiscriminently throws data at whatever has focus and is a really bad way to do this and very dangerous as you mentioned.

As devguru mentioned... sometimes asking if a different approach is called for. I'm absolutely doing that right here in this forum.  I'd love to get completely away from SendKeys... too many things that bother me about it. I don't mind using it to throw a Tab or Enter key or whatever but trying to use it for anything other than a single keystroke is very painful.

Some additional details... I do have the windows handles of the form and all of it's controls. This particular text box is a ThunderRT6TextBox. When I set focus to it and send a mouse click to the control the cursor begins in the middle of it... Keyboard is not able to get the cursor back to position 1 and line 1... no clue why. SO that's why I tab over to it.. that way the cursor is right where I need it and I can verify that that particular control actually has focus.  ANYWAY moving on...

WM_SETTEXT is a good suggestion.. I'll play around with it and see what I can come up with.. thanks for the link!  I'll get back to this thread and let yall know how that works out.
Excellent alternative to Sendkeys... Thanks so much!!