How to simulate mouse click?

Need help, or want to share a macro? Post here!
User avatar
HakanSeven12
Posts: 939
Joined: Wed Feb 06, 2019 10:30 pm

How to simulate mouse click?

Postby HakanSeven12 » Mon Mar 09, 2020 7:56 am

I want to simulate mouse click at given coordinate. How can I do that?
openBrain
Posts: 4885
Joined: Fri Nov 09, 2018 5:38 pm

Re: How to simulate mouse click?

Postby openBrain » Mon Mar 09, 2020 8:27 am

Unfortunately QTestEventList isn't available in PySide.
You could try to use QtGui.QApplication.sendEvent(), but I doubt it will really work. ;)
wmayer
Site Admin
Posts: 16666
Joined: Thu Feb 19, 2009 10:32 am

Re: How to simulate mouse click?

Postby wmayer » Mon Mar 09, 2020 9:42 am

HakanSeven12 wrote:
Mon Mar 09, 2020 7:56 am
I want to simulate mouse click at given coordinate. How can I do that?

Code: Select all

from PySide2 import QtCore
from PySide2 import QtGui
from PySide2 import QtWidgets

mw = Gui.getMainWindow()
gl = mw.findChild(QtWidgets.QOpenGLWidget)
me = QtGui.QMouseEvent(QtCore.QEvent.MouseButtonRelease, QtCore.QPoint(800,300), QtCore.Qt.LeftButton, QtCore.Qt.LeftButton, QtCore.Qt.NoModifier)

app = QtWidgets.QApplication.instance()
app.sendEvent(gl, me)
The position is relative to the GL widget.
User avatar
Kunda1
Posts: 8853
Joined: Thu Jan 05, 2017 9:03 pm

Re: How to simulate mouse click?

Postby Kunda1 » Mon Mar 09, 2020 12:57 pm

wmayer wrote:
Mon Mar 09, 2020 9:42 am
HakanSeven12 wrote:
Mon Mar 09, 2020 7:56 am
I want to simulate mouse click at given coordinate. How can I do that?
The position is relative to the GL widget.
Adding to Code snippets, yo!

Edit:
Done in Code snippets#How_to_Simulate_a_Mouse_Click_at_a_given_Coordinate
Want to contribute back to FC? Checkout:
#lowhangingfruit | Use the Source, Luke. | How to Help FreeCAD | How to report FC bugs and features
User avatar
HakanSeven12
Posts: 939
Joined: Wed Feb 06, 2019 10:30 pm

Re: How to simulate mouse click?

Postby HakanSeven12 » Mon Mar 09, 2020 1:24 pm

wmayer wrote:
Mon Mar 09, 2020 9:42 am
The position is relative to the GL widget.
Is there a way to use 3d view coordinates?
wmayer
Site Admin
Posts: 16666
Joined: Thu Feb 19, 2009 10:32 am

Re: How to simulate mouse click?

Postby wmayer » Mon Mar 09, 2020 1:41 pm

HakanSeven12 wrote:
Mon Mar 09, 2020 1:24 pm
wmayer wrote:
Mon Mar 09, 2020 9:42 am
The position is relative to the GL widget.
Is there a way to use 3d view coordinates?
If you have a 3d point and want to get the 2d point on the opengl widget then use this:

Code: Select all

from FreeCAD import Base

v = Gui.ActiveDocument.ActiveView
point3d = Base.Vector(x, y, z)
point2d = v.getPointOnScreen(point3d)
size = v.getSize()
coordX = point2d[0]
coordY = size[1] - point2d[1]

...
me = QtGui.QMouseEvent(QtCore.QEvent.MouseButtonRelease, QtCore.QPoint(coordX,coordY), QtCore.Qt.LeftButton, QtCore.Qt.LeftButton, QtCore.Qt.NoModifier)
User avatar
HakanSeven12
Posts: 939
Joined: Wed Feb 06, 2019 10:30 pm

Re: How to simulate mouse click?

Postby HakanSeven12 » Mon Mar 09, 2020 1:47 pm

wmayer wrote:
Mon Mar 09, 2020 1:41 pm
If you have a 3d point and want to get the 2d point on the opengl widget then use this:
Thank you :)