[Solved] Scroll-zoom-independent mouse coordinates

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
Limea
Posts: 2
Joined: Thu Jun 29, 2023 5:17 pm

[Solved] Scroll-zoom-independent mouse coordinates

Post by Limea »

Hi, I am totally newbie and I am trying to get a macro that print the mouse coordinates when the user clicks on the mouse.
I have seen there is a "MouseEvent" function that could help me:
https://api.libreoffice.org/docs/idl/re ... Event.html
But I have no clue how to use it. Could someone please send an easy example of a macro using this "MouseEvent" function, so that I can start experimenting with it.
I would really appreciate your help!
Last edited by MrProgrammer on Mon Jul 10, 2023 7:20 pm, edited 1 time in total.
Reason: Tagged ✓ [Solved] -- MrProgrammer, forum moderator
OpenOffice 4.1.12
JeJe
Volunteer
Posts: 2785
Joined: Wed Mar 09, 2016 2:40 pm

Re: Get mouse coordinates

Post by JeJe »

Example from Useful Macro Information
For OpenOffice.org
By
Andrew Pitonyak

(its a free download - you should read it for help)

Code: Select all

'14.3.7. Example 4: com.sun.star.awt.XMouseClickHandler
'This handler allows to intercept mouse clicks in a document.
'Listing 14.14: Complete mouse click handler.
Option Explicit
Global oDocView As Object
Global oMouseClickHandler As Object
Sub RegisterMouseClickHandler
 oDocView = ThisComponent.currentController
 oMouseClickHandler = _
 createUnoListener("MyApp_", "com.sun.star.awt.XMouseClickHandler")
' writedbginfo oMouseClickHandler
 oDocView.addMouseClickHandler(oMouseClickHandler)
End Sub
Sub UnregisterMouseClickHandler
 on error resume next
 oDocView.removeMouseClickHandler(oMouseClickHandler)
 on error goto 0
End Sub
Sub MyApp_disposing(oEvt)
End Sub
Function MyApp_mousePressed(oEvt) As Boolean
 MyApp_mousePressed = False
End Function
379
Function MyApp_mouseReleased(oEvt) As Boolean
Dim sMsg As String
 With oEvt
 sMsg = sMsg & "Modifiers = " & .Modifiers & Chr(10)
 sMsg = sMsg & "Buttons = " & .Buttons & Chr(10)
 sMsg = sMsg & "X = " & .X & Chr(10)
 sMsg = sMsg & "Y = " & .Y & Chr(10)
 sMsg = sMsg & "ClickCount = " & .ClickCount & Chr(10)
 sMsg = sMsg & "PopupTrigger = " & .PopupTrigger '& Chr(10)
 'sMsg = sMsg & .Source.dbg_Methods 
 End With
 
 ThisComponent.text.string = sMsg
 
 MyApp_mouseReleased = False
End Function
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Cazer
Posts: 53
Joined: Mon May 15, 2023 11:55 am

Re: Get mouse coordinates

Post by Cazer »

JeJe has given you a brilliant example. In this script, the 'RegisterMouseClickHandler' function is establishing a mouse click handler on the current document. When a click happens, it fires the 'MyApp_mouseReleased' function.

In 'MyApp_mouseReleased', the script captures the click event details and stores them in 'sMsg'. The details include the mouse coordinates (X, Y), which is exactly what you're after.

To use this, paste it into your macro editor and run the 'RegisterMouseClickHandler' function. After that, whenever you click in your document, the event details will replace the document's text.

Remember to run 'UnregisterMouseClickHandler' to stop the behavior when you're done.
OpenOffice 4.1.14
OS
Limea
Posts: 2
Joined: Thu Jun 29, 2023 5:17 pm

Re: Get mouse coordinates

Post by Limea »

This is very good, thanks a lot. It's almost doing exacly what I want.
The only point is that the coordinates are dependant of the position of the document in the window and of the zoom.
I suspect the coordinates to be defined in pixel. So maybe the coordinates are dependant of the resolution of the screen as well.
And I want coordinates that are independant of all this.
In other words: my documents contains a picture with different objects. I want the user of the macto to click on the object, so that the macro can get the coordinates of the objects. But this coordinates for one object have to be the same indepedantly of the fact that the user has scrolled down or zoomed in.
Any clue how can I get this?
OpenOffice 4.1.12
JeJe
Volunteer
Posts: 2785
Joined: Wed Mar 09, 2016 2:40 pm

Re: Get mouse coordinates

Post by JeJe »

this will get the coordinates of a selected picture object anchored to a paragraph in a Writer document. We can get the size directly but to get the position of the paragraph its anchored to we need to move the viewcursor to the paragraph temporarily. You may need to make adjustments for different anchoring positions. You should explore with MRI.

Code: Select all

controller = thiscomponent.currentcontroller
if thiscomponent.currentselection.supportsService("com.sun.star.text.TextGraphicObject") then
sel =thiscomponent.currentselection
sz=thiscomponent.currentselection.Size
controller.select thiscomponent.currentselection.getanchor.getstart
ps = controller.viewcursor.position
msgbox "X" & ps.x & chr(10) & "Y" &  ps.y & chr(10) & "width" & sz.width & chr(10) & "width" &  sz.height
controller.select sel
end if
Edit: and there may be more complexity if the object is anchored somewhere other than the main text.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
musikai
Volunteer
Posts: 294
Joined: Wed Nov 11, 2015 12:19 am

Re: Get mouse coordinates

Post by musikai »

This will be a difficult task.
Even the cursor x position is dependent on the canvas position or zoom. (= not reliable)
Then there are many ways the image can be set and positioned away from the anchor.
You could get the relative image position with HoriOrientPosition and VertOrientPosition but these are relative to HoriOrientRelation and VertOrientRelation. The image could be positioned relative to ,"Paragraph text area","Paragraph area","Left paragraph border","Right paragraph border","Left page border","Right page border","Page text area","Entire page","Character"


But perhaps the OP has an image that shows objects and so really want to have the mouse coordinates.
Here someone claims to have solved the problem how to get mouse coordinates relative to the document page:
https://stackoverflow.com/questions/443 ... asic-macro
Win7 Pro, Lubuntu 15.10, LO 4.4.7, OO 4.1.3
Free Project: LibreOffice Songbook Architect (LOSA)
http://struckkai.blogspot.de/2015/04/li ... itect.html
JeJe
Volunteer
Posts: 2785
Joined: Wed Mar 09, 2016 2:40 pm

Re: Get mouse coordinates

Post by JeJe »

Interesting link. I hadn't noticed the values are in the status bar - if that's visible then I can read them like this on OO:

Code: Select all

els =thiscomponent.currentcontroller.frame.layoutmanager.elements
for i = 0 to ubound(els)
if instr(1,els(i).resourceurl,"statusbar") then
msgbox els(i).realinterface.accessiblecontext.getaccessiblechild(7).gettext
exit for
end if
next
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Post Reply