Listener Works Unexpectedly

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
sokolowitzky
Posts: 103
Joined: Mon Sep 15, 2014 7:34 pm

Listener Works Unexpectedly

Post by sokolowitzky »

I am trying to write a macro to select ranges without the ctrl button.
The ranges don't have to be adjacent, once the selecting is done, I want copy the strings in the ranges, into the clipboard, without using ctrl button, or any keyboard input in general, and without making any change on the sheet.

I have set nearly all the macro necessary, without a problem. but the listeners go out of control.
I need a listener to keep former selected range address in memory and add the new selected range address into the former one.

a macro linked to selection change listener repeats itself more than one time as if ctrl is pressed, while the mouse cursor is moved it keeps chosing new ranges whereever the cursor is moved to.

how can I avoid this. I don't want to remove the listener, because it's supposed to work if I select a new range. but as summary, that code cause the openoffice work as if the ctrl is pressed.

Code: Select all

Option Explicit
 
Public  oListener As Object
Public  oDocView As Object
Public  oSelMem 'As Object

'run this macro to start event intercepting
Sub Example_SelectionChangeListener
  Dim sName$
  'Dim oSelMem
  oDocView = ThisComponent.getCurrentController
    
  'create a listener to intercept the selection change event
   sName = "com.sun.star.view.XSelectionChangeListener"
  oListener = CreateUnoListener( "MyApp_", sName )
    
  ' register the listener to the document controller
  oDocView.addSelectionChangeListener(oListener)
  
  oSelMem = Replace(ThisComponent.CurrentSelection.Absolutename, "$", "")
End Sub

'run this macro to stop event intercepting
Sub Remove_Listener
dim oDocView
  ' removes the listener
  ThisComponent.getCurrentController.removeSelectionChangeListener(oListener)
End Sub

'all listeners must support this event
Sub MyApp_disposing(oEvent)
  msgbox "disposing the listener"
End Sub

Sub MyApp_selectionChanged(oEvent)
Dim oarrow
oarrow = " <=> "
  Dim oCurrentSelection As Object
  Dim nCurSel
  Dim ros
  'the source property of the event struct 
  'gets a reference to the current selection
   oCurrentSelection =  oEvent.source
   'MsgBox oCurrentSelection.dbg_properties
   nCurSel = Replace(ThisComponent.CurrentSelection.Absolutename, "$", "")
   ros = right(oselmem, len(ncursel) )
'   msgbox ros & oarrow & ncursel
   if ros = ncursel then
'    msgbox  right(oselmem, len(ncursel)) = ncursel
'    msgbox ros = ncursel
'   Remove_Listener
oSelMem = oSelMem
   else
 oSelMem = oSelmem & chr(10) & nCursel   

 'msgbox """" & right(oselmem, len(ncursel)) & """"  & oarrow & """" & ncursel & """" & chr(10) & """" & oSelMem & """"
test_oSelMem
 end if
End Sub

Sub Test_oSelMem
msgbox oSelMem
End Sub
Win10-OpenOffice 4.1/LibreOffice 7.4
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Listener Works Unexpectedly

Post by Villeroy »

I am trying to write a macro to select ranges without the ctrl button.
Shift+F8 turns on multi-select mode. F8 turns it off.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
sokolowitzky
Posts: 103
Joined: Mon Sep 15, 2014 7:34 pm

Re: Listener Works Unexpectedly

Post by sokolowitzky »

sokolowitzky wrote: Sun Oct 30, 2022 2:47 pm without using ctrl button, or any keyboard input in general, and without making any change on the sheet.
Win10-OpenOffice 4.1/LibreOffice 7.4
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Listener Works Unexpectedly

Post by Villeroy »

RangesSelection.ods
(13.57 KiB) Downloaded 73 times
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
JeJe
Volunteer
Posts: 2787
Joined: Wed Mar 09, 2016 2:40 pm

Re: Listener Works Unexpectedly

Post by JeJe »

Don't know if it would work but you could try this:

Add a mouseclickhandler to the controller
On the mouse pressed event turn on multi-select mode


Edit: Why can't you just click the status bar to change to add selection mode and have a button on a toolbar once you've finished selecting to copy to the clipboard?
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Post Reply