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.
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
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.