Assign a shortcut for a macro

Shared Libraries
Forum rules
For sharing working examples of macros / scripts. These can be in any script language supported by OpenOffice.org [Basic, Python, Netbean] or as source code files in Java or C# even - but requires the actual source code listing. This section is not for asking questions about writing your own macros.
Post Reply
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Assign a shortcut for a macro

Post by JeJe »

OpenOffice's F5 only calls the first macro in a module.

This will allow you to assign a temporary shortcut to any macro.

Code: Select all


'Written with the help of code from AltSearch extension.
'https://extensions.openoffice.org/en/project/alternative-dialog-find-replace-writer-altsearch
Sub setMacroShortcut(applicationOrDocument,libraryname, Modulename, subOrFunctionName,modifiers, keycode)
	dim i as long,st as string, ky as new com.sun.star.awt.KeyEvent

	objModuleCfgMgrSupplier = createUnoService("com.sun.star.ui.ModuleUIConfigurationManagerSupplier")
'	ModuleCfgMgr = objModuleCfgMgrSupplier.getUIConfigurationManager("com.sun.star.text.TextDocument")
	ModuleCfgMgr = objModuleCfgMgrSupplier.getUIConfigurationManager("com.sun.star.script.BasicIDE")
	ShortCutMgr = ModuleCfgMgr.getShortCutManager
	ky.keycode =keycode
	ky.modifiers = modifiers
	
	allevents = ModuleCfgMgr.getShortCutManager.getAllKeyEvents
	for i = 0 to ubound(allevents)
		with allevents(i)
			if .modifiers = modifiers and .keycode = keycode then 			'.keyfunc
				ShortCutMgr.removeKeyEvent ky
				exit for
			end if

		end with
	next

	' "vnd.sun.star.script:LibName.ModuleName.SubName?language=Basic&location=application"
	st = "vnd.sun.star.script:" & libraryname &  "." & Modulename & "." & subOrFunctionName & "?language=Basic&location="  & applicationOrDocument
	ShortCutMgr.setkeyevent ky,st

End Sub
Edit:

Keycode constants and modifier constants are here:

http://www.openoffice.org/api/docs/comm ... t/Key.html

http://www.openoffice.org/api/docs/comm ... ifier.html


Example call:

setMacroShortcut "application","JeTest", "Module1","Test2", &h2,519 'Ctrl + h
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Post Reply