Library tutorials & articles

Simulating User Actions

Buttons being clicked

To simulate a user clicking a button, the code you need is more advanced... First, you need to get the hWnd of the button using the FindWindowEx API call. And to do this, you need to know its caption, and class name (always Button). For example, the following code finds a button with a caption "OK" within frmMain. If you don't know the hWnd of the form it is in, you need to find that as well!

lObjhWnd = FindWindowEx(frmMain.hWnd, 0, "Button", "OK")

Don't forget that many buttons will have 'access' keys so that you can click them using the keyboard. For example, if a button has a line under the letter C in its caption, you can click it by pressing Alt+t. So, if its caption appears to be "Cancel", but there is a line under the C, its caption is actually "&Caption" (just like in VB).

SendMessage lObjhWnd, BM_CLICK, 0, 0

Sometimes, you will find that the button must not have the focus before receiving the BM_CLICK message... you will just have to try and see. If this is the case, you can use the SetFocus API to set the focus to another item on the form:

SetFocus lOtherItemhWnd

The declarations you need for all this code are below.

Private Const BM_CLICK = &HF5
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Declare Function SetFocus Lib "user32" (ByVal hwnd As Long) As Long

Comments

  1. 29 Aug 2007 at 03:08
    This is col and all and im happy wit all the keys used i it, except for one, y is it that enter is not in the list when i type KeyPress(vbKey?
  2. 13 Apr 2007 at 21:03
    Hi, is there a C# version of this code, by the way thanks a lot for this code too, it helps me too much.

  3. 13 Sep 2006 at 07:51

    Will this code prevent the screensaver from beeing activated (if I just use the mouse movement now and then)?

    /Max

  4. 12 Feb 2005 at 21:20
    It might be a good idea to drop the second 'n' from ShiftOnn and CtrlOnn
  5. 07 Mar 2004 at 11:04

    I can't make it work.
    I'm using this code:

    Code:
    Private Sub tMover_Timer()
       Select Case tMover.Tag
       Case 0
           MoveMouse GetCurrentX + 10, GetCurrentY
           tMover.Tag = 1
       Case 1
           MoveMouse GetCurrentX + 10, GetCurrentY + 10
           tMover.Tag = 2
       Case 2
           MoveMouse GetCurrentX, GetCurrentY + 10
           tMover.Tag = 3
       Case 3
           MoveMouse GetCurrentX - 10, GetCurrentY + 10
           tMover.Tag = 4
       Case 4
           MoveMouse GetCurrentX - 10, GetCurrentY
           tMover.Tag = 5
       Case 5
           MoveMouse GetCurrentX - 10, GetCurrentY - 10
           tMover.Tag = 6
       Case 6
           MoveMouse GetCurrentX, GetCurrentY - 10
           tMover.Tag = 7
       Case 7
           MoveMouse GetCurrentX + 10, GetCurrentY - 10
           tMover.Tag = 0
       End Select
    End Sub

    To move the mouse around in a circle with a timer. But all it do is moving the mouse towards the bottom right corner. What am I doing wrong?

  6. 07 Mar 2004 at 10:53

    I can't make it work.
    I'm using this code:

    Code:
    Private Sub tMover_Timer()
       Select Case tMover.Tag
       Case 0
           MoveMouse GetCurrentX + 10, GetCurrentY
           tMover.Tag = 1
       Case 1
           MoveMouse GetCurrentX + 10, GetCurrentY + 10
           tMover.Tag = 2
       Case 2
           MoveMouse GetCurrentX, GetCurrentY + 10
           tMover.Tag = 3
       Case 3
           MoveMouse GetCurrentX - 10, GetCurrentY + 10
           tMover.Tag = 4
       Case 4
           MoveMouse GetCurrentX - 10, GetCurrentY
           tMover.Tag = 5
       Case 5
           MoveMouse GetCurrentX - 10, GetCurrentY - 10
           tMover.Tag = 6
       Case 6
           MoveMouse GetCurrentX, GetCurrentY - 10
           tMover.Tag = 7
       Case 7
           MoveMouse GetCurrentX + 10, GetCurrentY - 10
           tMover.Tag = 0
       End Select
    End Sub

    To move the mouse around in a circle with a timer. But all it do is moving the mouse towards the bottom right corner. What am I doing wrong?


    [edit] Nevermind. I thought that positions were absolute. They're relative

  7. 25 Nov 2003 at 20:38

    Many many thanks for this.


    I had found something somewhere else on calling API procedures but couldn't get it to work.


    I had been searching for ages for something like this and am very grateful for it.


    I was trying to create something in Excel that does a screen scrape of a payments system developed in-house, but one of the pages that contained the info I needed called for a right mouse click with no shortcut-key.  Another thing required a mouse-click into a field that had neither an accelerator nor was it a tab-stop.  So emulating a mouse move and click was the only thing I could do.


    Oh the possibilities!  


  8. 24 Jan 2003 at 16:46

    I wan to to do this but with a captionless button on crystal reports preview windows to force click event search button

  9. 01 Jan 1999 at 00:00

    This thread is for discussions of Simulating User Actions.

Leave a comment

Sign in or Join us (it's free).

James Crowley James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audience ...

Related podcasts

  • Christian Beauclair

    14 mai 2008 (�mission #0074) ::.Christian Beauclair: Stratégies de migration VB6 vers .NET Nous discutons avec Christian Beauclair des stratégies de migration VB6 vers .NET. Entre autres, nous discutons comment utiliser le "VB 6 Code Advisor" et le "Interop Forms Toolkit" pour ajouter la puiss...

Want to stay in touch with what's going on? Follow us on twitter!