2021-04-11

57: Registering Key Bindings for LibreOffice or OpenOffice

<The previous article in this series | The table of contents of this series | The next article in this series>

For invoking macros (in Python, Java, JavaScript, BeanShell, or Basic). Via extensions. Per document type.

Topics


About: LibreOffice
About: Apache OpenOffice
About: The Java programming language
About: The Python programming language
About: JavaScript
About: BeanShell
About: LibreOffice Basic
About: Apache OpenOffice Basic

The table of contents of this article


Starting Context



Target Context


  • The reader will know how to register key bindings for LibreOffice or Apache OpenOffice via extensions.

Orientation


There is an article on creating any LibreOffice or Apache OpenOffice extension that contains any Python macros.

There is an article on invoking any LibreOffice or Apache OpenOffice macro (including ones that are contained in extensions) from your program.


Main Body

Stage Direction
Here are Hypothesizer 7, Objector 57A, and Objector 57B in front of a computer.


1: The Motivation for Registering a Key Binding


Hypothesizer 7
In order to invoke a macro from the LibreOffice or Apache OpenOffice GUI, a way is to create a button on the document.

Objector 57A
Ah, a button on a spread sheet. But the macro is not exclusive for the spread sheet . . .

Hypothesizer 7
Sir, I understand.

Objector 57A
And I can't add a button on a Writer document, can I?

Hypothesizer 7
I do not say you cannot, but it may not be any satisfactory solution.

Objector 57A
As I can't freeze any part of the Writer document as I can do some rows of a Calc document, do I have to scroll to the button part every time when I need to invoke the macro?

Hypothesizer 7
I guess that that is not convenient.

Objector 57A
"not convenient" is a gross understatement, you know.

Anyway, even if the button is always visible, it is tiresome to move the mouse cursor up in order to click the button or even to grip the mouse every time I need to invoke the macro . . .

Hypothesizer 7
I understand.

Objector 57B
Can't I register a context menu item?

Hypothesizer 7
Ah, you can, madam, but context menu is for doing what depend on the context and requires mouse handling anyway and registering any context menu item requires some programming, so, here let us register key bindings, which is much easier.


2: Any Key Binding Can Be Registered Easily with an Extension


Hypothesizer 7
Any key binding can be registered easily with an extension.

Objector 57A
"extension"? I can't create any "extension".

Hypothesizer 7
Yes, you can; please read a previous article.

Objector 57A
. . . Um? Hmm, so, it's just a ZIP file?

Hypothesizer 7
Yes, it is a ZIP file, with the manifest, and a configuration file for the key bindings in this case.

Objector 57B
How about the macro? Can't I put also it into the extension?

Hypothesizer 7
Yes, you can; a previous article has already dealt with how to put any Python macro into the extension.

Objector 57B
"Python"? Who said "Python"?

Hypothesizer 7
I did, madam.

Objector 57B
I use Basic; I am supposed to use Basic, right?

Hypothesizer 7
Whatever supposed by someone does not matter; you decide what to do.

Objector 57B
Why shouldn't I use Basic?

Hypothesizer 7
Well, plainly speaking, because it is not good as a programming language.

Objector 57B
How "not good"?

Hypothesizer 7
It is so "not good" as I advise you to stay away from.

Objector 57B
. . . I'm asking 'in what way'.

Hypothesizer 7
It is ill-conceived, ill-equipped, and ill-documented.

Objector 57B
It's very ill . . .

But the LibreOffice GUI inevitably leads me to the Basic IDE when I try to edit macros!

Hypothesizer 7
Yes, the GUI is very misleading, being as though you have to create your macros in Basic, but that is not the case.

Objector 57B
But Basic seems handier because it has the IDE.

Hypothesizer 7
That IDE is . . . not-so-un-clumsy, I have to say. Why do you not use your favorite Python IDE instead?

Objector 57B
I use Basic, because everybody is using it.

Hypothesizer 7
Not "everybody" is using it.

Objector 57B
I am saying that I use Basic, for God's sake!

Hypothesizer 7
. . . All right, I will explain how to put any Basic macro into any extension, in a future article.


3: What to Write in the Configuration File


Hypothesizer 7
Anyway, the contents of the configuration file is like this.

KeyBindings.xml

@XML Source Code
<?xml version='1.0' encoding='UTF-8'?>
<oor:component-data xmlns:oor="http://openoffice.org/2001/registry" xmlns:xs="http://www.w3.org/2001/XMLSchema" oor:name="Accelerators" oor:package="org.openoffice.Office">
	<node oor:name="PrimaryKeys">
		<node oor:name="Modules">
			<node oor:name="com.sun.star.text.TextDocument">
				<node oor:name="C_MOD2" oor:op="replace">
					<prop oor:name="Command" oor:type="xs:string">
						<value xml:lang="en-US">vnd.sun.star.script:theBiasPlanet.clipboardHandlerUnoExtension.unoExtension.oxt/Scripts/python/theBiasPlanet/clipboardHandlerUnoExtension/macros/ClipboardHandler.py$copy?language=Python&amp;location=user:uno_packages</value>
					</prop>
				</node>
				<!-- Add each key as a node -->
			</node>
			<!-- Add each document type as a node -->
		</node>
	</node>
</oor:component-data>

Objector 57A
. . . Hmm, where is the key?

Hypothesizer 7
"C_MOD2" is the key, meaning 'Alt' + 'C'.

Objector 57A
So, does 'MOD1' mean 'Ctrl'?

Hypothesizer 7
Exactly.

Objector 57A
How about 'Shift' + 'Ctrl'?

Hypothesizer 7
'SHIFT_MOD1' and 'SHIFT_MOD2' mean 'Shift' + 'Ctrl' and 'Shift' + 'Alt'.

Objector 57B
"vnd.sun.star.script:theBiasPlanet.clipboardHandlerUnoExtension.unoExtension.oxt/Scripts/python/theBiasPlanet/clipboardHandlerUnoExtension/macros/ClipboardHandler.py$copy?language=Python&location=user:uno_packages" seems to mean a Python macro.

Hypothesizer 7
Yes, a user-owned in-extension Python macro to be exact. The expression for any macro of each type has been explained in a previous article.

Objector 57B
. . . Um? Oh. Also Basic is there.

Hypothesizer 7
An important point is "com.sun.star.text.TextDocument", which means that the key binding is valid only when any Writer document is active.

Objector 57A
Oh. Is it . . . 'com.sun.star.sheet.SpreadsheetDocument' for Calc?

Hypothesizer 7
You guessed right.

Objector 57A
It must be 'com.sun.star.presentation.PresentationDocument' for Impress.

Hypothesizer 7
. . . I am amazed that you guessed right again.

Objector 57A
. . . Is it 'com.sun.star.graphics.GraphicDocument' for Draw?

Hypothesizer 7
Well, it is 'com.sun.star.drawing.DrawingDocument', actually.

Objector 57A
Huh? I don't get it. The document type is 'Graphics', right?

Hypothesizer 7
Right, but I cannot change the fact.


References


<The previous article in this series | The table of contents of this series | The next article in this series>