Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Throttling sendkeys vb.net

Posted on 2011-10-27
4
Medium Priority
?
779 Views
Last Modified: 2012-08-14
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

0
Comment
Question by:romieb69
  • 2
4 Comments
 
LVL 40
ID: 37042200
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

0
 
LVL 21

Accepted Solution

by:
developmentguru earned 2000 total points
ID: 37042283
I have not been a VB programmer for many years, but I am a long time Windows developer.  Personally I would use the Windows API functions to get the handle of the TextBox.  With the handle of the TextBox you should be able to send a WM_SETTEXT message containing all of the text and it should simply set it all at one, no send keys, no throttling, no mistakes.

You find the top level window handle like this (excerpted from EE question 21842805):

If FindByClass = True Then
    ' if you want to find a window by its class ID
    WinHandle = FindWindow("Window class", vbNullString)
Else
    ' if you want to find a window by its caption
    WinHandle = FindWindow(vbNullString, "Window Caption")
End If

A response further down the line (ID:16670475 Author:iHadi) shows some use of the EnumChildWindows to find the handle of the TextBox.

Once you have the window handle you can send the message WM_SETTEXT to set the text directly, no send keys.  Here is the MSDN link which includes VB.NET code:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms632644(v=vs.85).aspx

Often, as a programmer, you need to step back from what you are doing and ask if a different approach is called for...
0
 

Author Comment

by:romieb69
ID: 37044816
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.
0
 

Author Closing Comment

by:romieb69
ID: 37050993
Excellent alternative to Sendkeys... Thanks so much!!
0

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Loops Section Overview

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question