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

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