Hi,
I've retrieved a routine that I used in my package
Vocabulary in order to set
a keyboard-shortcut, so I've thought to share it as a
code-snippet.
regards
Paolo
<?xml version="1.0"?>
<!--
$RCSfile: $
last change: $Revision: $ $Author: $ $Date: $
(c)2003 by the copyright holders listed with the
author-tags.
If no explicit copyright holder is mentioned with a certain
author,
the author him-/herself is the copyright holder. All rights
reserved.
Public Documentation License Notice:
The contents of this Documentation are subject to the
Public Documentation License Version 1.0 (the
"License");
you may only use this Documentation if you comply with
the terms of this License. A copy of the License is
available at http://ww
w.openoffice.org/licenses/PDL.html
The Original Documentation can be found in the CVS archives
of openoffice.org at the place specified by RCSfile: in this
header.
The Initial Writer(s) of the Original Documentation are
listed
with the author-tags below.
The Contributor(s) are listed with the author-tags below
without the marker for being an initial author.
All Rights Reserved.
-->
<snippet language="OOBasic"
application="Office">
<keywords>
<keyword>keybinding</keyword>
<keyword>keyboard shortcut</keyword>
<keyword>XAcceleratorConfiguration</keyword>
<keyword>UIConfigurationManager</keyword>
</keywords>
<authors>
<author id="paolomantovani"
initial="true" email="paolomantovani openoffice.org">Paolo
Mantovani</author>
</authors>
<question heading="Managing keyboard
shortcuts">How do I manage keyboard shortcuts for a
given document type ?
</question>
<answer>
<p>The example code is a subroutine that shows several
techniques in order to manage keybindings.</p>
<p>The purpose of the macro is to set a key binding
(CTRL+T) in order to launch a macro
(Standard.Module1.Main)</p>
<p>The code checks initially if the keybinding is
already used.</p>
<p>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.</p>
<p>Notice the syntax used for the macro URL:</p>
<p>vnd.sun.star.script:Standard.Module1.Main?language=
Basic&location=application</p>
<p>that follows the specificatios of the new OOo
scripting framework (from OOo 2.0.x)</p>
<listing>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}&qu
ot;)
' Retrieve the module configuration manager
with module identifier
oModuleCfgMgr =
oModuleCfgMgrSupplier.getUIConfigurationManager("{<
img src="/img/at.gif" align="middle" border="0" alt="">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?"
sMsg = GetResString(1026) & Chr(10)
sMsg = sMsg & sLocCommand &
"""." & Chr(10)
& Chr(10)
sMsg = sMsg & GetResString(1027)
iMsgResult = MsgBox( sMsg, 1)
If iMsgResult = 1 Then
oWriterShortCutMgr.removeKeyEvent(
aKeyEvent)
oWriterShortCutMgr.setKeyEvent( aKeyEvent,
sCommand )
oWriterShortCutMgr.store
End If
End Select
End Sub</listing>
</answer>
<versions>
<version number="1.0.x" status="can
not work"/>
<version number="1.1.x" status="can
not work"/>
<version number="2.0.x"
status="tested"/>
</versions>
<operating-systems>
<operating-system name="All"/>
</operating-systems>
<changelog>
<change author-id="paolomantovani"
date="2006-03-23">Initial
version</change>
</changelog>
</snippet>
------------------------------------------------------------
---------
To unsubscribe, e-mail: dev-unsubscribe api.openoffice.org
For additional commands, e-mail: dev-help api.openoffice.org |