Automating OpenOffice: Customizing Key Strokes

How to Add Assign Macros in OpenOffice.org Documents

© Mark Alexander Bain

It is possible to assign macros to various keys in OpenOffice, but not all of them. However, introducing a Listener allows a user to customize any key stroke.

One of the great strengths of OpenOffice.org is that even novice users can start customizing the application for their own (or other peoples') needs. Typically the process will be something like:

These OpenOffice customizations can be one of four types:

The act of customization is carried out through OpenOffice's "Customize" dialog (which is accessible from the main menu by clicking on "Tools" and then "Customize"). However, there are limitation to what can be customized through the dialog; for example it is not possible to assign the keystrokes Control+P to anything other than printing the document. For that the spreadsheet developer must turn back to writing some Basic code; and that code involves introducing an OpenOffice key handler.

Adding a Key Handler to an OpenOffice.org Document

There are two steps to creating an OpenOffice key handler:

Fortunately the code to add the listener is not too complicated:

Option Explicit

Global oKeyHandler

Sub AddKeyHandler

oKeyHandler = CreateUnoListener("KeyHandler_","com.sun.star.awt.XKeyHandler")

thisComponent.currentController.addKeyHandler(oKeyHandler)

End Sub

This piece of code is simple, but carries out some key jobs:

The first of the macros (and the one that will do all of the important work) has to named in a specific way:

The code is therefore:

Function KeyHandler_keyPressed(oKeyEvent as new com.sun.star.awt.KeyHandler) As Boolean

If oKeyEvent.modifiers = com.sun.star.awt.KeyModifier.MOD1 Then

If oKeyEvent.keyCode = com.sun.star.awt.Key.P Then

KeyHandler_keyPressed = True 'Turn off default action

msgbox "No; I won't print today, thank you!"

End If

End If

End Function

This macro processes the key presses passed to it, and there are two types of key types that it's looking for:

The macro above simply looks for MOD1 and P (i.e. control+P), and then outputs a message box instead of the default print dialog; while the next macro simply returns the application to it's default state (i.e. just waiting for another key to be input):

Function KeyHandler_keyReleased(oKeyEvent as new com.sun.star.awt.KeyHandler) As Boolean

KeyHandler_keyReleased = False

End Function

And the third macro doesn't actually do anything at all - it's there for completeness and that's all:

Sub KeyHandler_disposing

End Sub

Removing a Key Handler from an OpenOffice.org Document

As well as adding a key hander, it may be useful to remove it as well - for example, the user may want to disable Control+P sometimes, but enable it at others; so this final macro does just that - removes the handler:

Sub RemoveKeyHandler

thisComponent.currentController.removeKeyHandler(oKeyHandler)

End Sub

The end result is a simple, but effective, set of macros that extends the users ability to customize OpenOffice even further.


The copyright of the article Automating OpenOffice: Customizing Key Strokes in Office/Business Software is owned by Mark Alexander Bain. Permission to republish Automating OpenOffice: Customizing Key Strokes in print or online must be granted by the author in writing.





Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo