Remap Home key in Firefox and only when in text input box?

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.

Is that doable?

You will have to test if the following idea works or not, because I don't have FF. It might work.

Create a macro with a hotkey of Home. Use this code in the macro:

image

thanks again for your assistance. but this still is active throughout the Firefox browser, even when I'm not in the edit area.

I don't know, but could it be that “Select All” is always active? Even in non-text fields?

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.

Not in my tests with the apps I use. But FF is a poorly behaved app, so I'm not surprised.

Ok, I was just guessing because that's how it is in other browsers. :slightly_smiling_face:

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.


Go Back with Backspace Macro (v11.0.3)

Go Back with Backspace.kmmacros (19 KB)

Firefox doesn't support scripting:

So KM cannot send javascript to it as it does for those browsers that do, like Safari or Chrome,:

https://wiki.keyboardmaestro.com/actions/Execute_a_JavaScript_in_Browser

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.

https://wiki.keyboardmaestro.com/trigger/URL

You'll need to create the macro that handles the Home key.

My javascript is close to nil, but here is a snippet to get you started:

// ==UserScript==
// @name Home Key Remap in Text Field
// @match :///*
// @grant none
// @run-at document-start
// ==/UserScript==

(function() {
document.addEventListener('keydown', function(e) {
if (e.key === 'Home') {
const el = document.activeElement;
const isText = el && (
el.tagName === 'TEXTAREA' ||
(el.tagName === 'INPUT' && ['text', 'search', 'email', 'url', 'tel', 'password'].includes(el.type)) ||
el.isContentEditable
);

        if (isText) {
            e.preventDefault(); // Stop default Home behavior

            window.location.href = "kmtrigger://macro=HomeKeyInTextField";
            // Call the Keyboard Maestro macro, HomeKeyInTextField
        }
    }
});

})();

  1. Paste the snippet into your newly created userscript.
  2. Make a macro to handle the HOME key.
  3. Name it: HomeKeyInTextField.
    (If you want spaces in the title, just be sure to URL Encode the name. See the previous wiki link).
  4. 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

Good luck!

1 Like

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.

image

1 Like

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?

That was the clue I needed. It works when I search on menu item with Undo. That is only available in the text input field. Thanks!

Override Home in Text Field.kmmacros (3.4 KB)

Oh wow. Great catch!