I'm trying to remap the Home key to ⌘+← (Command + Left Arrow) only when I'm in a text input field inside Firefox — just like macOS behavior in many apps. I can set up a macro to perform that override for all of FF, which I don't want. Just when I'm in a text edit area.
Yeah, could be. After looking at other messages in this forum, I think what I need to look at is looking in the DOM and setting a variable with the element. I'm too much of a novice with javascript or DOM to figure it out, but I may try a few things. Otherwise I just need to remember not to use Home and End, but rather arrow left and right.
If I understand correctly, you need “something” that decides whether you are in a text field or not. As far as I know, KM can't do that. Other apps can.
But maybe it works if you search for a menu item in Firefox that is only active when you need it.
I have this Macro I use in the Plex app to use the backspace key to go back a page only when I'm not in the text field. Something like this should work for you. Just replace "Plex" with "Firefox" in the AppleScript and remove the part of the macro that isn't useful to you.
However, if you're open to using a Firefox add-on, there are some that allow users to run custom JavaScript on web pages—essentially mimicking browser scripting capabilities. Here are a few:Tampermonkey, Violentmonkey, and Greasemonkey.
Once you've downloaded, installed and created a new userscript, you could create a javascript event listener for the Home key that checks if the active element is a text input, if so, stops the default Home behavior and calls a km macro, via URL Trigger, to handle the HOME key.
if (isText) {
e.preventDefault(); // Stop default Home behavior
window.location.href = "kmtrigger://macro=HomeKeyInTextField";
// Call the Keyboard Maestro macro, HomeKeyInTextField
}
}
});
})();
Paste the snippet into your newly created userscript.
Make a macro to handle the HOME key.
Name it: HomeKeyInTextField.
(If you want spaces in the title, just be sure to URL Encode the name. See the previous wiki link).
Make sure to save the userscript. Go to a fresh webpage or refresh an existing page to load the script. Type the HOME key in a text field in Firefox. It should call the macro.
Alternatively, replace the kmtrigger line with javascript that moves the cursor to beginning of the selection.
const pos = el.selectionStart;
const value = el.value || '';
const before = value.slice(0, pos);
const lineStart = before.lastIndexOf('\n') + 1;
el.selectionStart = el.selectionEnd = lineStart;
// Move cursor to beginning of line
I have the ESR version of Firefox. When not in a text field, Edit > Cut is available for use. In a text field, it's grayed out unless some text is selected. If the Cut menu behaves the same in other versions of Firefox then that menu condition should work using the Keyboard Maestro If Then Else action.
Ahhh. That's interesting. So do you think there are any menu tricks (or any tricks at all) for detecting if the text cursor is flashing inside a text field?
What does the Copy menu item do in FF if the text cursor is not active in a text field? Does it copy anything? If not, there may be away to solve this by doing a copy prior to checking the menu, with a little pause in between.
Across all apps? I don't think so. Or not that I know of. This forum sees enough times when people are trying to determine if they are in a text field, and it seems like, at best, there is sometimes a solution depending on the app.
Nothing gets copied despite the menu item being enabled.
Based on that advice, we can get closer by performing Select All, Copy, Left Arrow, and see if anything is in the clipboard, but there are 2 problems with this: it won't work if the text field is empty, and it will deselect anything that is selected. So I guess that won't work very well for FF.
I don't get what the AXTextField in the If All Condition part does? I see where you're setting Plex_Text but not where AXTextField is set. Is that the string that will be returned from the AXFocusedUIElement script?