Log In    |    Knowledge Base    |    4D Home
Tech Tip: How to simulate mouse click and key press by using POST EVENT command
PRODUCT: 4D Developer | VERSION: 11 | PLATFORM: Mac & Win
Published On: May 14, 2008

How to simulate mouse click event by using POST EVENT command. First create a button with coordinates:

By using GET MOUSE(mouseX;mouseY,mouseButton) we will get the current mouse coordinates and store them to mouseX and mouseY variables. In the button's Property List window, Events section, we check the "On Mouse Enter" event, all other are unchecked. Then we check the current mouse pointer coordinates against button coordinates ie. we check if the mouse pointer is over the button and if it is true the POST EVENT command is executed and the alert window will pop up on the screen to show than the event was triggered.

The following is the actual object method for the button:


GET MOUSE(mouseX;mouseY;mouseButton)
If ((mouseX>63) & (mouseY<350))
  If ((mouseY>127) & (mouseY<323))

    POST EVENT(Mouse down event ;0;Tickcount;mouseX;mouseY;0)
    ALERT("You made it")
  End if
End if

Also, it is possible to simulate pressing key from the keyboard. This example shows how we can simulate holding the Shift key so we can enter upper case character. For the purpose of this example we have created a form with 2 object variables(Variable1 and Variable2). Variable1 will show the entered lower case character and Variable2 will show the uppercase case character right after the POST EVENT command triggered. Also, in the property list for object Variable1 check in On Load and On Getting Focus events.

The object method for Variable1is as follows:

Variable1:=Request("enter a lower case character")

POST EVENT(Key down event ;Ascii(Variable1);Tickcount;0;0;Shift key mask )

Parameter Result
Key down event Will simulate pressing down the key from the keyboard.
Ascii(Variable1) Hold the lower case character entered in dialog window.
Shift key mask Will simulate "holding the Shift key".

The list of all modifiers is on the following link:

Constants for Events(Modifiers)