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?
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?
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.
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:
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.
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.
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
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.
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.