[Solved] Continuously record mouse position in sheet

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
MrSpeed
Posts: 2
Joined: Sat Mar 16, 2019 3:18 am

[Solved] Continuously record mouse position in sheet

Post by MrSpeed »

Hi all, I am trying to convert a macro from Excel VBA to Calc Basic. This is my first venture into Calc Basic.

The macro records the Y mouse position in a cell then moves down one cell and records the next mouse position. There is a 100 milli second delay between recordings. It works but only when mouse movements are not continuous.

The full code further below is mostly declarations. The actual recording of the mouse is done by:

Code: Select all

GetCursorPos lngCurPos   REM gets the mouse from Windows
CellRange.value = lngCurPos.y    REM places the Y position in a cell. 
I have the problem in Calc Basic only, not in the original VBA. In Calc Basic as long as the mouse is moving, the GetCursorPos is halting the macro. Only when the mouse stops for a brief moment the position is passed on and the macro continues. However I want to capture the mouse position while the mouse is continuously moving. Anybody any idea? The mouse will be moving all the time.

Code: Select all

REM  *****  BASIC  *****
Option Explicit
Declare Sub      Sleep      Lib "C:\WINDOWS\system32\kernel32.dll" Alias "Sleep" ( ByVal dwMilliseconds As Long)
Public Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long

Public Type POINTAPI
x As Long
y As Long
End Type
 
Sub LogYMousePosition()
Dim lngCurPos As POINTAPI
Dim Doc As Object
Dim Sheet As Object
Dim CellRange As Object
Dim i as Long 

i=1
Doc = ThisComponent
Sheet = Doc.Sheets.getByName("Sheet1")
CellRange = Sheet.getCellByPosition(0,i)   

Do
    GetCursorPos lngCurPos
    CellRange.value = lngCurPos.y
    i=i+1
    CellRange = Sheet.getCellByPosition(1,i)
    Sleep 100
    if i=70 then i=0  rem for testing only
    
Loop

End Sub
Many thanks in advance.
Last edited by MrSpeed on Sat Mar 16, 2019 2:56 pm, edited 1 time in total.
OpenOffice 4.1.6 on Windows 10
User avatar
Zizi64
Volunteer
Posts: 11352
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: continuously record mouse position in sheet

Post by Zizi64 »

The programming interface of the Excel and the AOO/LO are very different.

The StarBasic programming language and the IDE of the AOO/LO is very simple to use. But you must often call a few ones of the thousands of the API functions. API: Application Programming Interface.
The functions of the MS VBA and the AOO/LO API are basicly different. You need rewrite all of the macros written in VBA.

You can use one of the excellent (third party) object inspection tools like the MRI or the XrayTool to examine the properties and methods of the programming objects.

And you can try the LibreOffice. The LO has a littlebit higher compatibility with the foreign file formats and the MS VBA macros.
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: continuously record mouse position in sheet

Post by JeJe »

Openoffice provides you with listeners which means you can do these things directly without resorting to the Windows api.

There are mouse listeners which will give you information about the mouse location and also selection change listeners and many others. Access to these is a big advantage with OO.

Quickly finding a couple of threads which may be in the region of what you want to do:

viewtopic.php?f=45&t=60504

viewtopic.php?f=20&t=42487
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: continuously record mouse position in sheet

Post by JeJe »

Looking at your code - Change sleep 100 to Wait 100

(Edit:
in my first comment I was assuming you wanted to record the mouse position in the calc window for some purpose of working with calc. If you want to record the mouse position generally then you can't do that with an OO listener, and need your method)
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
MrSpeed
Posts: 2
Joined: Sat Mar 16, 2019 3:18 am

Re: Continuously record mouse position in sheet

Post by MrSpeed »

:D Sleep to Wait did the trick! Thanks JeJe!! So I guess the program halted on sleep then? I don't fully understand but it works. And Thanks Zizi64 for the tips. Good to know.
OpenOffice 4.1.6 on Windows 10
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: [Solved] Continuously record mouse position in sheet

Post by JeJe »

Yeah, I don't know why sleep doesn't work here either, but its generally better to use an inbuilt function anyway.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Post Reply