Page 1 of 1

[Python] Get current document from keyevent

PostPosted: Fri Apr 16, 2021 4:12 pm
by Bastien
Hi everybody,

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

Code: Select all   Expand viewCollapse view
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.

Re: [Python] Get current document from keyevent

PostPosted: Fri Apr 16, 2021 4:36 pm
by Villeroy
and what kind of animal is Source? Does it have an ImplementationName or something?

Re: [Python] Get current document from keyevent

PostPosted: Fri Apr 16, 2021 4:47 pm
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?

Re: [Python] Get current document from keyevent

PostPosted: Fri Apr 16, 2021 4:48 pm
by Bastien
I'd say it's something like ".uno.XInterface" ? Which has :

Code: Select all   Expand viewCollapse view
(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?

Re: [Python] Get current document from keyevent

PostPosted: Fri Apr 16, 2021 4:52 pm
by JeJe
As I said the first item in that list... AccessibleContext... use that... or don't because ThisComponent does the job...

Re: [Python] Get current document from keyevent

PostPosted: Fri Apr 16, 2021 5:00 pm
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.

Re: [Python] Get current document from keyevent

PostPosted: Fri Apr 16, 2021 5:08 pm
by RoryOF
Then you need to insert code something like
Code: Select all   Expand viewCollapse view
ctx = XSCRIPTCONTEXT.getComponentContext()
smgr = ctx.getServiceManager()

Re: [Python] Get current document from keyevent

PostPosted: Fri Apr 16, 2021 5:13 pm
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   Expand viewCollapse view
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).

Re: [Python] Get current document from keyevent

PostPosted: Fri Apr 16, 2021 6:17 pm
by JeJe
In Basic, with a slightly different route to the titledbordertext, something like this.
Code: Select all   Expand viewCollapse view



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


Re: [Python] Get current document from keyevent

PostPosted: Fri Apr 16, 2021 7:44 pm
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()