Delay between KeyStroke

Discussion in 'Programming & Scripting' started by dschu012, Feb 3, 2009 with 3 replies and 211 views.

  1. dschu012

    dschu012 Member

    Messages:
    760
    Ratings:
    381
    I am sending keystroke to a webBrowser control. The problem is that I need the program to act more humanlike and type the keystrokes are a human speed. Another problem is that I do not want to use Thread.Sleep(###); because it prevents other things from getting updated such as the webBrowser control. I've tried using a timer control but I can't seem to get it to work like I want it to have a small delay random between each keystroke. Any tips from people that have experience with a timer?

     
  2. Ibdc

    Ibdc Retired Retired

    Messages:
    5,100
    Ratings:
    2,072
    I know you said you don't want to use thread.sleep, but if you use Application.DoEvents before sleeping it keeps the application responsive. Play around with it a little.

    Here's an example
    Code:
    TextBox1.Focus();
    SendKeys.Send("l");
    Application.DoEvents();
    System.Threading.Thread.Sleep(500);
    SendKeys.Send("o");
    Application.DoEvents();
    System.Threading.Thread.Sleep(500);
    SendKeys.Send("l");
    
    This will give focus to textbox1 (make it active) and then type "lol" waiting half a second in between each keystroke, while keeping the application responsive.
     
  3. OP
    dschu012

    dschu012 Member

    Messages:
    760
    Ratings:
    381
    Still seems to make the application seem quite unresponsive. I would think that there would be a better way.

    Edit:
    After all my google searching it seems that I have found a suitable answer. What I did is I put all of my keys into a queue and then used a Timer control. In the Event for the timer I would do
    SendKeys.Send(keyQueue.Dequeue());

    So every 500ms the event will get fired typing in the next key while not hanging the program.
     
  4. GoldBl4d3

    GoldBl4d3 Retired Retired

    Messages:
    3,758
    Ratings:
    1,328
    thats becuase applications.doevents is a method that make the lines of code above to to fully complete every task need to do before executing the next line. These task include, redrawing, loading and more.

    thats another reason you see

    progressbar1.value++;
    Appliication.DoEvents();

    becuase it waits for everything to finish loading and redrawing before continuing.