fushar
fushar

Reputation: 398

How to emulate mouse click using wxWidgets

Is there any function in wxWidgets framework, say, click(), whose function is to emulate a left-click mouse?

Upvotes: 2

Views: 2404

Answers (3)

Pouria P
Pouria P

Reputation: 595

You can use the wxControl::Command() method. According to Documentation:

Simulates the effect of the user issuing a command to the item.

Documentation page

List of events to use with wxCommandEvent constructor

You can simulate a click on a button like this:

$this->MyButton->Command(new wxCommandEvent(wxEVT_COMMAND_BUTTON_CLICKED));

There is also the wxUIActionSimulator class that you can use to simulate UI actions.

Upvotes: 1

VZ.
VZ.

Reputation: 22678

No, there is no function like this. If you really need to do it, e.g. because you want to perform some action in another application you need to write platform-specific code yourself (e.g. use SendInput() under Windows).

If you want to use this to execute some code in your own application though, there is a much better solution: instead of doing unsupported, fragile and ugly

void MyClass::OnLeftUp(wxMouseEvent& event)
{
   ... do something with click at event.GetPosition() ...
}

void MyOtherFunction()
{
  wxMouseEvent event(...);
  ... initialization not shown because there is no right way to do it anyhow ...
  myObject->ProcessEvent(event);
}

just refactor your code to do it like this instead:

void MyClass::OnLeftUp(wxMouseEvent& event)
{
   HandleClick(event.GetPosition());
}

void MyClass::HandleClick(const wxPoint& pos)
{
   ... do something with click at pos  ...
}

void MyOtherFunction()
{
  myObject->HandleClick(position);
}

Upvotes: 2

RyanWilcox
RyanWilcox

Reputation: 13974

If you're just talking about emulating a click on another button in your wxWidgets app, I've tried this before:

Create a fake wxEvent of the right type, tell the event what object to care about using SetEventObject(), and tell a parent wxWindow in the hierarchy to ProcessEvent(myNewEvent). You might want to use wxGetTopLevelParent() to get the topmost frame/dialog

If you are talking about emulating a click in another, non-wxWidgets process, it's possible you could use the OS's accessibility APIs to do this. wxAccessibility should be able to help with this on Windows -- for other OSes, last I heard (granted, a few years ago), you'll have to use the native OS functions.

Upvotes: 1

Related Questions