【Input Monitor】Text Selection AND Character Before the Cursor

Will KM support monitor text selection AND character before the cursor ?
That would be useful for switching Input Method.

This is what can be done in Emacs, but I want the function system-wide:

2022-02-19 05:08:01 +0800

This is what I got so far:

input-monitor.kmmacros.zip (4.5 KB)

PLEASE DISABLE it before use/review it.
It may be helpful for someone who want to monitor text input and change input method.

Switch Input Method by Application is simple but stupid.
If you have hundreds of apps, you have to take care of all these apps, but if we just monitor the text selection and character before the cursor, Switching Input Method will be simple and universal for all text area.

@peternlewis Will KM consider add trigger by text selection and trigger by character before the cursor/caret (pre-cursor character) ?
That would be helpful for non-English user switching Input Method.

Or more simple (difficult?): get the situation of whether we are in Text Edit Field/Area?

This is how to do it in Vim:

I want that power with KM.

I haven't been responding to your thread because I'm not sure if I understand it, since I haven't used emacs in decades.

However I'm not sure what you mean by "trigger by character before the cursor". KM can trigger only by hardware or software events. So maybe what you meant to ask was "Can KM access the previous character in the text field when the user triggers a macro with a keyboard key."

For KM to be able to access that character, there has to be an API in MacOS to get it. There may very well be an API for that. And if so, the character can probably be accessed by AppleScript. And if so, KM doesn't need a new trigger, all you need to know is how to use KM to get the last character. I can research that for you. There's probably a way. But I'm not very good with AppleScript so it may take some time.

You also asked if KM can determine if "whether we are in a Text Edit Field Area." That's a fair question. Once again, that requires MacOS being able to provide that information. I can think of an easy way in KM to get that information. That is, if your cursor is in a text edit area, then you can determine that with this statement.

Does this meet your needs? It seems to work for me. If the Paste menu item exists, then we are in a Text box. Do you have any apps for which this command doesn't work?

The method of getting the character next to the cursor is extremely application dependent. There is no "system wide" method. Each app that runs on MacOS is free to use its own dictionary methods for external apps like KM for retrieving that data.

However you can probably trick most apps by copying the previous character using the standard MacOS keys (Shift Left Arrow, CMD-C, Right Arrow.) Would you be satisfied with this trick?

There is a way to get whether a user is in text edit area:

FelixKratz/SketchyVim: Turns macOS input fields into real vim buffers

The difficult part is to detect whether in text selection mode or cursor mode, then we can use keystroke and clipboard to get the target character, instead of doing the copy thing all the time.

Okay. But why go to all that work when the KM action above seems to work?

I didn't get you.

I set a notification, but it seems didn't work.

Well, first of all, I said to look for the word "Paste" but you used the word "Edit". That's a different word, with a totally different meaning in the menus. Secondly, I don't know what those oriental character mean, so I will have to ignore them. Thirdly, your macro works for me when I fix it like this:

The pause at the beginning is to give me enough time to manually switch to a different app and click inside a text field.

I'm sleepy right now, as always, so I might have to wait until tomorrow.

Thank you very much, I will try it again.

I made the exact same macro as yours, still didn't succeed.
Maybe I should use trigger by keystroke because before I send Chinese char to screen I always have to press Space or Enter or number to choose from list.
I will find another way out.

Probably not.

There is no system event that can be used to detect these things.

It's not entirely clear to me how that works, but I have tested the accessibility APIs used in that code to get the focussed text selection or get/set text, and they do not work reliably across applications.

I find a way to get pre-cursor char/word, the first step towards succeed.

Next, I need to figure out how to change Input-Method based on Current Language & Space char number & Current Input-Method.

My brain is in a mess because of all those situations:
In English or Chinese, they both have space char, my first thought is changing IM using two space-char input/regex-check trigger

This is 100 dollar.
这是 100 元。

Than you for your awesome KM!

Will KM support detect what Input Method is using?
Currently, KM only support set/change IM instead of get current IM in using.

Some code may help:

About Accessibility, this may help:

Can you use this in KM to get the keyboard layout? It seems to work for me. but I see your point, there doesn't seem to be an official action to get this information. It's a fair question.

This also seems to work:

1 Like

Thank you very much.
I do searched this before and have some code in my MD notes.
It would be better if KM can add it as trigger.

If KM could add it as a token that would be quite reasonable. But as a trigger, that would require a MacOS event for KM to be able to implement. I don't know if MacOS provides a system event for that. I'd guess it doesn't.

I think I succeeded with your help.
Thank you very much!

Noted.

2 Likes