The example code is a subroutine that shows several techniques in order to manage keybindings.
The purpose of the macro is to set a key binding (CTRL+T) in order to launch a macro (Standard.Module1.Main)
The code checks initially if the keybinding is already used.
In this case the code asks to the user if he wants to change the key and, if yes, the old keybinding is removed and replaced with the new one.
Notice the syntax used for the macro URL:
vnd.sun.star.script:Standard.Module1.Main?language=Basic&location=application
that follows the specificatios of the new OOo scripting framework (from OOo 2.0.x)
Sub SetUpKeyBinding
Dim oModuleCfgMgrSupplier As Object
Dim oModuleCfgMgr As Object
Dim oWriterShortCutMgr As Object
Dim sCommand As String
Dim sLocCommand As String
Dim sMsg As String
Dim iMsgResult As Integer
' Initialize strings
sCommand = "vnd.sun.star.script:Standard.Module1.Main?language=Basic&location=application"
' Retrieve the module configuration manager from central module configuration manager supplier
oModuleCfgMgrSupplier = createUnoService("{@see com.sun.star.ui.ModuleUIConfigurationManagerSupplier}")
' Retrieve the module configuration manager with module identifier
oModuleCfgMgr = oModuleCfgMgrSupplier.getUIConfigurationManager("{@see com.sun.star.text.TextDocument}")
oWriterShortCutMgr = oModuleCfgMgr.getShortCutManager
Dim aKeyEvent As New {@see com.sun.star.awt.KeyEvent}
With aKeyEvent
.Modifiers = {@see com.sun.star.awt.KeyModifier:MOD1} 'API const for the CTRL key = 2
.KeyCode = {@see com.sun.star.awt.Key:T} 'API const for the T key = 531
End With
On Error Resume Next
sLocCommand = oWriterShortCutMgr.getCommandByKeyEvent(aKeyEvent)
On Error GoTo 0 'restore the error handler
Select Case sLocCommand
Case = "" 'no previous bindings
oWriterShortCutMgr.setKeyEvent( aKeyEvent, sCommand )
oWriterShortCutMgr.store
Case = sCommand 'ok the key event is already used by our command
'nothing to do
Case Else 'the key event is already used by another command
sMsg = "La combinazione di tasti ""CTRL+T"" è già usata per il comando:" & Chr(10)
sMsg = sMsg & sLocCommand & """." & Chr(10) & Chr(10)
sMsg = sMsg & "Si desidera ugualmente usare questa combinazione per lanciare Standard.Module1.Main?"
iMsgResult = MsgBox( sMsg, 1)
If iMsgResult = 1 Then
oWriterShortCutMgr.removeKeyEvent( aKeyEvent)
oWriterShortCutMgr.setKeyEvent( aKeyEvent, sCommand )
oWriterShortCutMgr.store
End If
End Select
End Sub