Trapping keystrokes after trigger

I have a macro activated by a hot key when MS Word is active. The first two things it does are to use a short AppleScript to get the current paragraph's style name form Word and then to show a dialog with that style name displayed in a field.

The problem is that it takes a second or so for Word to respond to the AS request, and in that time I may have typed a few characters which are intended for KM's dialog but which end up in the Word document instead.

Is there a way of getting KM to trap all keystrokes after a trigger has been typed?


Probably not in a way that would work for you.
You could display a KM Prompt for User Input with a text input field, and that should start receiving you keystrokes. Just tested this in Word and it seems to work.

However, that will Pause the KM Macro at that Prompt Action until you cancel or OK it.
I suppose you could put this single action in another Macro which you call asynchronously.

But this all feels like a terrible cluge to me that is not very reliable.

If your processing is entirely within MS Word, then I would suggest that you use either Word VBA or an AppleScript only solution. I have not tested this, but I would suspect that a Word VBA would respond instantly to getting the current paragraph style.

Once you have the style, what do you want to do with it?

Being forced to run Catalina on my new MBP16 has forced me to abandon Office 2011. Word 2011 had a feature I used a lot: after a trigger key, I could type a style name and it would be applied to the current paragraph. If the name was the same as the existing style, Word would give me the option to “reapply the existing style” or “redefine the style based on the current settings”. I found this really handy and used it a lot. I can’t find a way to do anything like it in Office 365 (or whatever MS are calling it this week).

So I’m trying to re-create it in KM and it’s going well.

  1. Get style name from Word using AS.

  2. Put up dialog requesting new style name, with old style as default. Dialog has buttons “redefine” and “apply” (and of course “cancel”).

  3. If redefine is clicked and the name is unchanged, use AS to tell Word to redefine the style.

  4. If apply is clicked, apply the style.

The only issue is the short time it takes for KM to run the script to get the current style from Word. I’m a fast typist, so I type the trigger and start to type the style name immediately. When it was all handled by Word 2011, that wasn’t a problem. Now, it is.

The only solution which occurs to me would be, as you suggest, to run the stage 1 script while the stage 2 dialog was on screen, but it doesn’t work. I tried an entirely AS-based solution, but it was even slower.

I suspect I’ll just have to restrain my over-eager fingers.


Here's a simple VBA macro to get you started.
Works with Word 365.

Sub Change_Style()
' Change_Style Macro
Dim curStyle As Style
Dim curPara As Paragraph

Dim styleName As Variant

Set curStyle = Selection.Style
styleName = curStyle.NameLocal

Debug.Print styleName

styleName = InputBox("Enter New Style", "Change Style", styleName)

Debug.Print styleName

Selection.Style = ActiveDocument.Styles(styleName)

Exit Sub

'--- Examples of Setting Style (NOT Used in this macro) ---

    ActiveDocument.Styles("Book Title").BaseStyle = "Default Paragraph Font"
    With ActiveDocument.Styles("Book Title").Font
        .Name = ""
        .Size = 20
        .Bold = True
        .Italic = True
        .Spacing = 0.25
    End With
    Selection.Style = ActiveDocument.Styles("Book Title")
    Selection.Style = ActiveDocument.Styles("Heading 1")
End Sub

Sorry - I’d missed this post. Thanks, Michael. Food for thought.


FWIW, I solved this problem by using a combination of FastScripts and KM. If the dialog is brought up by FS, it pops onscreen so quickly that even with rapid typing, keystrokes after the trigger go to the dialog and not to Word.