Ask Your Question
1

How to add Shortcut Key Combinations to Keyboard Customization

asked 2017-09-24 19:59:23 +0100

chiropterist gravatar image

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 http://ask.libreoffice.org/en/questio...]

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
0

answered 2017-09-26 14:53:10 +0100

librebel gravatar image

Hello @chiropterist,

+1 For nice question :)

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/en/question/132538/how-to-add-shortcut-key-combinations-to-keyboard-customization/

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 ...
(more)
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

2 followers

Stats

Asked: 2017-09-24 19:59:23 +0100

Seen: 551 times

Last updated: Sep 26 '17