[Python] Get current document from keyevent

Creating a macro - Writing a Script - Using the API
Post Reply
Bastien
Posts: 61
Joined: Mon May 12, 2014 2:45 pm

[Python] Get current document from keyevent

Post by Bastien »

Hi everybody,

I'm looking for the current document from a key event.

Code: Select all

class KeyHandler(unohelper.Base, XKeyHandler):

   def disposing(self, ev):
        pass

    def keyPressed(self, ev):

       if ev.KeyCode == PAUSE:

            # here I need o access the current document to manipulate text
            
        else:
            return False

        return True

    def keyReleased(self, ev):
        return False
By instrospecting 'ev' I get a 'source' but I can't see how to reach my current document.
LibreOffice 6.2.3.2 on Ubuntu 19.04
User avatar
Villeroy
Volunteer
Posts: 30805
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: [Python] Get current document from keyevent

Post by Villeroy »

and what kind of animal is Source? Does it have an ImplementationName or something?
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: 2065
Joined: Wed Mar 09, 2016 2:40 pm

Re: [Python] Get current document from keyevent

Post by JeJe »

The source is the Window which receives the key event... you could use the accessible context to go up the chain of accessible parent windows until you get the titledbordertext for the frame and then enumerate all the frames to find the document.

But as this should be the same as ThisComponent why not use that?
Openoffice 4.1.6
Windows 8
Bastien
Posts: 61
Joined: Mon May 12, 2014 2:45 pm

Re: [Python] Get current document from keyevent

Post by Bastien »

I'd say it's something like ".uno.XInterface" ? Which has :

Code: Select all

(Name)             (Value Type)                       (Value)      (Info.)   (Attr.)    (Handle)  
AccessibleContext  .accessibility.XAccessibleContext  -INTERFACE-  Pseud     Read_Only       
Background         long                                            Pseud     WriteOnly       
ControlFont        .awt.FontDescriptor                             Pseud     WriteOnly       
DesignMode         boolean                            False        Pseud                     
Enable             boolean                                         Pseud     WriteOnly       
FloatingMode       boolean                                         Pseud     WriteOnly       
FontDescriptors    [].awt.FontDescriptor              -Sequence-   Pseud     Read_Only       
Foreground         long                                            Pseud     WriteOnly       
Graphics           .awt.XGraphics                     -void-       Pseud     Read_Only       
Group              [].awt.XWindow                                  Pseud     WriteOnly       
ImplementationId   []byte                             -SEQUENCE-   Pseud     Read_Only       
Info               .awt.DeviceInfo                    -STRUCT-     Pseud     Read_Only       
MinimumSize        .awt.Size                          -STRUCT-     Pseud     Read_Only       
OutputSize         .awt.Size                          -STRUCT-     Pseud                     
Pointer            .awt.XPointer                                   Pseud     WriteOnly       
PosSize            .awt.Rectangle                     -STRUCT-     Pseud     Read_Only       
PreferredSize      .awt.Size                          -STRUCT-     Pseud     Read_Only       
Properties         [].beans.Property                  -Sequence-   Pseud     Read_Only       
Size               .awt.Size                          -STRUCT-     Pseud     Read_Only       
StyleSettings      .awt.XStyleSettings                -INTERFACE-  Attr.     Read_Only       
Toolkit            .awt.XToolkit                      -INTERFACE-  Pseud     Read_Only       
Types              []type                             -Sequence-   Pseud     Read_Only       
Visible            boolean                            True         Pseud                     
Windows            [].awt.XWindow                     -Sequence-   Pseud     Read_Only       

Is it relevant?
LibreOffice 6.2.3.2 on Ubuntu 19.04
JeJe
Volunteer
Posts: 2065
Joined: Wed Mar 09, 2016 2:40 pm

Re: [Python] Get current document from keyevent

Post by JeJe »

As I said the first item in that list... AccessibleContext... use that... or don't because ThisComponent does the job...
Openoffice 4.1.6
Windows 8
Bastien
Posts: 61
Joined: Mon May 12, 2014 2:45 pm

Re: [Python] Get current document from keyevent

Post by Bastien »

JeJe wrote:As I said the first item in that list... AccessibleContext... use that... or don't because ThisComponent does the job...
I dont have ThisComponent in this imported module.
LibreOffice 6.2.3.2 on Ubuntu 19.04
User avatar
RoryOF
Moderator
Posts: 33649
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: [Python] Get current document from keyevent

Post by RoryOF »

Then you need to insert code something like

Code: Select all

 ctx = XSCRIPTCONTEXT.getComponentContext()
 smgr = ctx.getServiceManager()
Apache OpenOffice 4.1.13 on Xubuntu 22.04.1 LTS and very infrequently on Win2K/XP
Bastien
Posts: 61
Joined: Mon May 12, 2014 2:45 pm

Re: [Python] Get current document from keyevent

Post by Bastien »

XSCRIPTCONTEXT is not available in an imported module.

The solution I found (to avoid launching multiple time the same keyhandler attachment) is to save the component in a class property.

Code: Select all

class KeyHandler(unohelper.Base, XKeyHandler, metaclass=Singleton):

    components = set()

    def __init__(self, ctx, component):
        self.ctx = ctx
        KeyHandler.components.add(component)

   (...)

    @property
    def mission(self):
        self.smgr = self.ctx.ServiceManager
        self.desktop = self.smgr.createInstanceWithContext(
            "com.sun.star.frame.Desktop", self.ctx)
        return self.desktop.getCurrentComponent()


Then I can work on the current component (called here 'mission) using a property. I'm pretty sure it's not the best solution. It should be possible to get the current component straight from the KeyEvent (which was my initial question).
LibreOffice 6.2.3.2 on Ubuntu 19.04
JeJe
Volunteer
Posts: 2065
Joined: Wed Mar 09, 2016 2:40 pm

Re: [Python] Get current document from keyevent

Post by JeJe »

In Basic, with a slightly different route to the titledbordertext, something like this.

Code: Select all




title= source.gettoolkit.getactivetopwindow.accessiblecontext.titledbordertext
 
 frames = stardesktop.getframes
 for i = 0 to frames.count -1
 if frames(i).title = title then
 component =frames(i).getcontroller.model
 exit for
 end if
 next

Openoffice 4.1.6
Windows 8
User avatar
Villeroy
Volunteer
Posts: 30805
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: [Python] Get current document from keyevent

Post by Villeroy »

If the calling object is a window object, you have attached the listener to that object. Is there any controller availlable? A controller also has XKeyHandler and from there you get the document via obj.getModel()
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
Post Reply