Capturing keydown event of MS Word

隐身守侯 提交于 2019-11-28 14:31:28

At last I try use autohotkey to solve this question,some code like down:


     #IfWinActive,ahk_class OpusApp

      ;回车键
      enter::
         send {enter}
         checkStyle()
         return

     backspace::
         send {backspace}
         checkStyle()
         return



     checkStyle(){
    word:=ComObjActive("word.application")
    if(word.Selection.Style.NameLocal="ListItemStyle" and word.Selection.Range.ListFormat.ListString = "")
    {
        word.Selection.Style := "someStyle"
        TrayTip, hint, style chnaged, 3, 17
    }
     }

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!