Results 1 to 7 of 7

Thread: What is the Best Way to Send Keys to Webbrowser?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2013
    Posts
    29

    What is the Best Way to Send Keys to Webbrowser?

    I'm currently trying to get my vb.net program to send simulate text entry to fill out forms in the webbrowser. I would normally just set the value of the webbrowser elements, but unfortunately the fields I'm trying to fill aren't in html, they're in a web-based Java Applet. Basically I have designed a robot to set focus to the webbrowser, and tab through the text fields entering data into each field. The data is numerical.

    I have tried three methods to do this:

    First I used SendKeys.Send(string) to send the values, but for some reason it consistently sends too many characters. For example instead of sending 83.1 it'll type 888833.1111. So obviously that's not good.

    Then I did a little research and found that this is an occasional problem with sendkeys, so I tried implementing some code I found for this function called InputSimulator. It works well when I execute InputSimulator.SimulateKeyPress(key), but when I try sending strings via InputSimulator.SimulateTextEntry(string) it doesn't send anything at all.

    Finally I tried some code that was given to me by a member of this forum.

    Code:
    For Each c In string
                If Not Char.IsNumber(c) AndAlso Char.ToUpper(c) = c Then keybd_event(VK_LSHIFT, 0, 0, 0)
                keybd_event(VkKeyScan(c), 0, 0, 0)
                keybd_event(VkKeyScan(c), 0, 2, 0)
                If Not Char.IsNumber(c) AndAlso Char.ToUpper(c) = c Then keybd_event(VK_LSHIFT, 0, 2, 0)
            Next
    Now this function types the numbers very consistently which is exactly what I'm looking for. However it doesn't seem to include the decimal, so a string 83.1 will get typed 831.

    So does anybody have any suggestion on what's the best way to approach this? I'm so close with this last method, I just need to include the punctuation it seems. Or is there perhaps some other, robust way to send keystrokes.

    Thanks,
    Michael

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: What is the Best Way to Send Keys to Webbrowser?

    Sorry. Didn't take account of the need for decimal notation. Do you need anything other than ".", specifically anything that needs SHIFT to type?
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  3. #3
    Member
    Join Date
    Sep 2012
    Posts
    46

    Re: What is the Best Way to Send Keys to Webbrowser?

    I think there may be a way to put information into a textbox, then once you have the field selected on the browser, to input the text from the textbox into the browser. I'll look around for you!

  4. #4
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: What is the Best Way to Send Keys to Webbrowser?

    Quote Originally Posted by DesignLife View Post
    I think there may be a way to put information into a textbox, then once you have the field selected on the browser, to input the text from the textbox into the browser. I'll look around for you!
    Er , no we already exhuasted those possibilities. As is stated in the OP, this is not a standard HTML form we are dealing with but a Java applet so there is no possibility of selecting the field in the manner you suggest.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Mar 2013
    Posts
    29

    Re: What is the Best Way to Send Keys to Webbrowser?

    I appreciate your help dunfiddlin, I owe you one anyways as your solution was the only one that worked. I'm amazed at how there are no very robust ways out there to send keys... Well yes, for right now the decimal is the only symbol that I really need.
    Perhaps later though, depending on how much I decide to automate, there could arise the need for some symbols such as apostrophes or @.

    Thanks again!

  6. #6
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: What is the Best Way to Send Keys to Webbrowser?

    No bother. Just a small adjustment. You can reduce the number of characters checked to suit your needs and obviously check that your keyboard layout actually matches!

    vb.net Code:
    1. If Char.IsLetter(c) AndAlso Char.ToUpper(c) = c OrElse "!""£$%^&*()_+{}@~<>?¬".Contains(c) Then keybd_event(VK_LSHIFT, 0, 0, 0)
    2.             keybd_event(VkKeyScan(c), 0, 0, 0)
    3.             keybd_event(VkKeyScan(c), 0, 2, 0)
    4.             If Char.IsLetter(c) AndAlso Char.ToUpper(c) = c OrElse "!""£$%^&*()_+{}@~<>?¬".Contains(c) Then keybd_event(VK_LSHIFT, 0, 2, 0)
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Mar 2013
    Posts
    29

    Re: What is the Best Way to Send Keys to Webbrowser?

    Thanks a bunch, that tweak worked beautifully... I wish I understood how this worked a little better though, I look at the code but I just cant seem to wrap my head around it. All I've really done is wrapped your code in a function and call it when needed. But thanks again!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width