Capturing keydown event of MS Word

前端 未结 2 867
旧时难觅i
旧时难觅i 2020-12-12 04:09

I want to capture the backspace event, just do the backspace\'s action, then add other action, but I am not sure the backspace\'s original action:Selection. Delete , -1 ?

2条回答
  •  爱一瞬间的悲伤
    2020-12-12 04:35

    It's a little kludgy, but it works:

    Public blnCatchBackspace As Boolean
    
    Public Sub CatchBackspace()
        If blnCatchBackspace Then
            ToggleBackspaceCatch 'Remove the backspace key binding
                SendKeys "{BACKSPACE}"
                DoEvents
                'insert code here
            ToggleBackspaceCatch 'Reapply the backspace key binding
        End If
    End Sub
    
    Public Sub ToggleBackspaceCatch()
        Dim lngProtection As Long
    
        With ActiveDocument
            'key binding will error if the document is protected
            'save the current protection state and unprotect the document
            lngProtection = .ProtectionType
            If lngProtection <> wdNoProtection Then .Protect wdNoProtection
        End With
    
        With Application
            If blnCatchBackspace Then 'clear the key binding
                blnCatchBackspace = False
                FindKey(wdKeyBackspace).Clear
            Else 'apply the key binding
                blnCatchBackspace = True
                .CustomizationContext = ActiveDocument
                .KeyBindings.Add KeyCategory:=wdKeyCategoryMacro, _
                    Command:="CatchBackspace", _ 
                    KeyCode:=BuildKeyCode(wdKeyBackspace)
            End If
        End With
        'reapply the original document protection
        ActiveDocument.Protect lngProtection
    End Sub
    

提交回复
热议问题