I want to run macro when it is not in text input mode

Is there a way to run a macro only when I'm not in text input mode?
If this becomes possible, I will be able to register various shortcuts using a single letter of the alphabet.
In fact, various websites and apps implement many single-letter shortcuts.
For example, pressing "N" on Twitter will create a new tweet, or pressing "/" on the Google search results screen will move the cursor to the search bar.
If the shortcut activates while inputting text, there is no point as I will not be able to input the text properly.
Can you get my point across?

That's a fair and reasonable question. I'm fairly sure KM doesn't have an existing built in condition that can deactivate the macro when macOS is in some text input mode, so the solution would probably have to trigger on every text key, (which is possible) and then the solution would check to see if macOS is in text input mode, and if it is, let the keys be sent to macOS, but if it is not, perform some action.

Moreover I suspect you still want function keys (ie, anything key that isn't a text key) to continue to trigger macros whether macOS is in text input mode or not, right?

I suspect that AppleScript is the most likely way to solve this, which means I'm not able to help.

I just googled "keyboard maestro determine if text input mode" and found this thread, which seems to have a solution.

I don't think that thread directly answers your question, but it seems to indicate a way of determining whether macOS is in text input mode, and someone could combine that with my idea in my first paragraph to get you a solution.

I like your idea, and the challenge of solving this. I'll read the web page I cited above and see if I can get it to work, but it involves AppleScript and I may not be above to solve it therefore.

Also, the thread indicated that the solution may have to work differently for different apps, so it would really help if you indicate what the app(s) is/are you want to get it to work with.

1 Like

I was trying to come up with a hack that might help you. You see, we can see with our eyes that when a text field is activated in macOS, if the mouse is over the text field (which is not always the case) then the mouse pointer on the screen turns into a vertical text bar. I'm sure there must be an API somewhere that can be used to determine what the current mouse pointer looks like. And sure enough, I found another thread online discussing this issue. Unfortunately I don't even recognize the language being used in the example. It might be Cocoa or Carbon or something else.

Even if it's Carbon or Cocoa, there might be a way for KM's Javascript action to access the same API. And that's way beyond me to help with. But it's an interesting approach that has limitations but is more likely to not be app-dependent, such as an AppleScript approach would likely be. So I'm just mentioning this approach as a possible direction for further consideration.

1 Like

Thank you so much Airy.
It seems very difficult to understand at my level as I don't know Apple Script or Cocoa, but your answer is very helpful.

In some contexts, you can use AppleScript to determine the current focus AS a text field, but not all. I have an action group favourite that does this, and can send it on Thursday if nobody else does beforehand.

1 Like

Thank you so much noisneil. I would love to see your method.

Give this a try:

Text Field Focused? (AS).kmmacros (39 KB)

Macro screenshot

2 Likes

Thank you very much noisneil!!
Wow great, correctly different notification messages were displayed in text input mode and in non-text input mode.
However, I'm sorry, but I don't know much about it, so I have one question.
I don't know how to set it up when actually applying this.
For example, suppose I want to do something like this:

" When not in text input mode, pressing '1' will behave like 'Command + D' "

Of course, this is completely different, right?


Just replace the "is NOT" notification action with your simulated keystroke. At the moment, you've placed it outside of the If/Else switch.

1 Like

Sorry for the elementary question. As you said, it was easy to do just by replacing it.
Then, actually, when I press a key, there is a slight delay due to (maybe) processing.
This will cause some stress when entering text, but this is probably unavoidable?
But I'm very grateful anyway. Thank you noisneil.

The Eleventh Commandment:

"Thou shall not use single character hotkey triggers in applications that accept typing."

Just don't. It's not good.