Conditionally catch CTRL-S

Add-in Express™ Support Service
That's what is more important than anything else

Conditionally catch CTRL-S
 
gjvdkamp




Posts: 56
Joined: 2018-08-28
Hi, I want to handle CTRL-S in certain conditions, else just let Excel continue with the normal behaviour of CTRL-S like the add-in doesn't exist.

I've setup a shortcut to catch CTRL-S, but how do I pass it on to Excel if I do not want to handle it?
Posted 18 Jun, 2021 03:11:06 Top
Andrei Smolin


Add-in Express team


Posts: 18793
Joined: 2006-05-11
Hello GJ,

Add-in Express sets a hook to intercept keyboard presses. There's a problem with this approach: when the user presses a keyboard shortcut and the hook informs you about this, the object model may not be prepared to incoming calls; we had such issues.

Accordingly, Add-in Express processes the hook message so that if there's an ADXKeyboardShortcut component handling a given shortcut, it uses the SendMessage/OnSendMessage mechanism that we describe in section https://www.add-in-express.com/docs/net-office-tips.php#wait-a-little to invoke that ADXKeyboardShortcut; this allows the ADXKeyboardShortcut.Action event to be handled when the object model is ready. Using an ADXKeyboardShortcut and, consequently, the SendMessage/OnSendMessage machinery implies leaving the hook message not cancelled as most keyboard shortcuts are not cancelled.

To let you cancel the keyboard shortcut, the algorithm above calls the ADXAddinModule.OnKeyDown event. Since it is called synchronously, you must not use any object model calls in an event handler of this event.

private void AddinModule_OnKeyDown(object sender, ADXKeyDownEventArgs e)
{
    // Set ADXAddinModule.HandleShortcuts = true for this event to be raised

    // You MUST NOT use any Office object model in this method.

    // https://docs.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes
    if (e.Ctrl && e.VirtualKey == 0x53) // stands for Ctrl+S
    {        
        e.Handled = true; // true - cancel the keyboard shortcut
    }
}


Private Sub AddinModule_OnKeyDown(sender As Object, e As ADXKeyDownEventArgs) Handles MyBase.OnKeyDown
    ' Set ADXAddinModule.HandleShortcuts = true for this event to be raised

    ' You MUST NOT use any Office object model in this method.

    ' https://docs.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes
    If e.Ctrl AndAlso e.VirtualKey = &H53 Then ' stands for Ctrl+S
        e.Handled = True ' true - cancel the keyboard shortcut
    End If
End Sub


UPDATE: The hook is set for the Office application, not for any specific window. Accordingly, you may encounter issues with that keyboard shortcut pressed in a non-generic scenario. I would recommend that you make sure your event handler correctly determines the context. Say, that shortcut can be pressed on a pane (custom or built-in), in the VBA editor, etc.; the list is obviously incomplete. You should check these scenarios, too.


Andrei Smolin
Add-in Express Team Leader
Posted 18 Jun, 2021 04:52:08 Top
gjvdkamp




Posts: 56
Joined: 2018-08-28
Excellent, thanks Andrei!
Posted 23 Jun, 2021 12:42:48 Top