How to add Shortcut Key Combinations to Keyboard Customization

I want to assign “Ctrl±” and “Alt±” to macros that insert an en-dash and an em-dash, respectively, in all LO modules.

By default “Ctrl±” is assigned in Writer to insert a soft hyphen and in Calc to delete cells, but as I rarely use these functions, I’m fine with reassigning the key combinations.

The problem is that “Alt±” doesn’t appear in the list of Shortcut Keys; same for “Alt+Shift±”, “Ctrl+Shift±”, and Ctrl+Alt+Shift±". Why? Can I add them? How?

[A similar question was asked, but not answered, at Possible to add new key combinations to keyboard shortcuts?]

Hello @chiropterist,

+1 For nice question :slight_smile:

Some points on beforehand:

  1. Certain Key combinations such as “ALT±” are not listed among the available Shortcut Keys in "Tools : Customize... : Keyboard", but can still be assigned to a command by using the API.
    Doing so is of course at ones own responsibility, since these Key combinations were probably left out of the GUI for a reason… Even after assigning a command to such Key combinations by API, they are still not visible in the GUI; but they are truly active and they do trigger the assigned command. Such ‘hidden’ Shortcut Keys can only be removed again by using the API.

  2. For Key combinations such as “CTRL±” that are already defined at the Module-level ( i.e. Writer, Calc ) like you mentioned, it would not be sufficient to install a Shortcut at the Global level for them, since they would be overruled by the Module’‘s Shortcut every time. Instead the Module’'s Shortcut must be overwritten for each Office Module separately. Seeing that you already agreed to that in your question, i wrote a macro here below that does just that.

  3. I have tested the macro below, and it works wonderfully in Writer, Calc, Draw, Impress, Formula Editor, the BasicIDE, but with one notable exception: it doesn’‘t seem to do anything in LibreOffice Base.
    I haven’'t looked into that yet, but i guess it could be solved by adjusting the method type_A_Character() to address the case for Base especially.

To try this macro, just copy-paste the code below into a Basic module inside your [My Macros and Dialogs].Standard library, then adjust the 2 constants in the method installMyShortcuts() to point to that location, then Run the method installMyShortcuts().

Hope it helps, lib

Sub installMyShortcuts()
REM Assigns 2 Keyboard shortcuts "Ctrl+-" and "Alt+-", to 2 macros that insert an en-dash and an em-dash respectively.
REM Call this method once to install the 2 Keyboard shortcuts in every Office Module.
REM	To remove these 2 Keyboard shortcuts again, set the below constant bInstall to <False>, then call this method once.
REM See: https://ask.libreoffice.org/t/how-to-add-shortcut-key-combinations-to-keyboard-customization/27914

REM ******************************************************************************************************************

	Const bInstall As Boolean = True		REM True = install the 2 shortcuts; False = remove the 2 shortcuts again.

REM *******  NB  ********     Set here the correct URL of your macro to be called when ALT-Minus is pressed:
	Const strCommandURL1$	= "vnd.sun.star.script:Standard.Module1.type_An_EMDASH?language=Basic&location=application"
	
REM *******  NB  ********     Set here the correct URL of your macro to be called when CTRL-Minus is pressed:
	Const strCommandURL2$	= "vnd.sun.star.script:Standard.Module1.type_An_ENDASH?language=Basic&location=application"
	
REM ******************************************************************************************************************

On Local Error Resume Next

	Dim oKeyEvent1 As New com.sun.star.awt.KeyEvent						REM KeyEvent representing ALT-Minus.
	oKeyEvent1.Modifiers	= com.sun.star.awt.KeyModifier.MOD2
	oKeyEvent1.KeyCode		= com.sun.star.awt.Key.SUBTRACT
	
	Dim oKeyEvent2 As New com.sun.star.awt.KeyEvent						REM KeyEvent representing CTRL-Minus.
	oKeyEvent2.Modifiers	= com.sun.star.awt.KeyModifier.MOD1
	oKeyEvent2.KeyCode		= com.sun.star.awt.Key.SUBTRACT
	
	Dim oShortcutManager As Object
	Dim oUIConfigurationManager As Object
	Dim oUIConfigurationManagerSupplier As Object
	oUIConfigurationManagerSupplier = getDefaultContext().getByName( "/singletons/com.sun.star.ui.theModuleUIConfigurationManagerSupplier" )
	
	Dim oModuleManager As Object
	oModuleManager = createUnoService( "com.sun.star.frame.ModuleManager" )
	
	Dim i As Integer
	Dim aModules() As String  : aModules = oModuleManager.getElementNames()
	
	For i = 0 To uBound( aModules )		REM install/remove the 2 Shortcuts in all available Office Modules:
				
		oUIConfigurationManager = oUIConfigurationManagerSupplier.getUIConfigurationManager( aModules( i ) )
		oShortcutManager 		= oUIConfigurationManager.getShortCutManager()
	
		If bInstall Then
			oShortcutManager.setKeyEvent( oKeyEvent1, strCommandURL1 )			REM Install the ALT-Minus Shortcut.
			oShortcutManager.setKeyEvent( oKeyEvent2, strCommandURL2 )			REM Install the CTRL-Minus Shortcut.
		Else
			oShortcutManager.removeCommandFromAllKeyEvents( strCommandURL1 )	REM Remove the ALT-Minus Shortcut again.
			oShortcutManager.removeCommandFromAllKeyEvents( strCommandURL2 )	REM Remove the CTRL-Minus Shortcut again.
		End If
		
		oShortcutManager.store()
	Next i
	
End Sub

Sub type_A_Character( iUnicode As Integer )
REM Types a Unicode character into the current window.
	Dim oWindow As Object
	oWindow = StarDesktop.ActiveFrame.getContainerWindow()
	
	Dim oKeyEvent As New com.sun.star.awt.KeyEvent
	oKeyEvent.Modifiers	= 0
	oKeyEvent.KeyChar	= iUnicode
	oKeyEvent.Source	= oWindow
	
	Dim oToolkit As Object
	oToolkit = oWindow.getToolkit()
	oToolkit.keyPress( oKeyEvent )		REM methods of XToolkitRobot.
	oToolkit.keyRelease( oKeyEvent )
End Sub

Sub type_An_EMDASH()
	type_A_Character( 8212 )	REM 8212 is the Unicode for the EM-DASH symbol "--".
End Sub

Sub type_An_ENDASH()
	type_A_Character( 8211 )	REM 8212 is the Unicode for the EN-DASH symbol "-".
End Sub


REM Fin