admin管理员组

文章数量:1289875

I'm working on a website where each page has buttons, like "submit" and "cancel". Each of these buttons has an accesskey attribute. The submit button's accesskey attribute is set to S, and the cancel button's accesskey attribute is set to C.

Access keys are activated using different modifiers in different browsers. Internet Explorer, Safari, and Google Chrome use just the alt, while Firefox uses both the alt and the shift keys. As well, Firefox uses alt + s to open the history menu.

Answers to this question should not suggest changing settings in the browser's configuration as that would not be feasible on a production site.

How I change the modifier keys that Firefox uses for accesskey's to just alt, and prevent the history menu from opening?

I am working in Ubuntu 16.04.

I'm working on a website where each page has buttons, like "submit" and "cancel". Each of these buttons has an accesskey attribute. The submit button's accesskey attribute is set to S, and the cancel button's accesskey attribute is set to C.

Access keys are activated using different modifiers in different browsers. Internet Explorer, Safari, and Google Chrome use just the alt, while Firefox uses both the alt and the shift keys. As well, Firefox uses alt + s to open the history menu.

Answers to this question should not suggest changing settings in the browser's configuration as that would not be feasible on a production site.

How I change the modifier keys that Firefox uses for accesskey's to just alt, and prevent the history menu from opening?

I am working in Ubuntu 16.04.

Share Improve this question edited Feb 24, 2017 at 4:16 user4639281 asked Feb 7, 2017 at 10:15 bob martibob marti 1,5693 gold badges13 silver badges27 bronze badges 5
  • If the existing answers don't answer your question, please explain how, keeping in mind that not everything is possible, and if this isn't, that's a valid answer. – anon Commented Mar 30, 2017 at 19:45
  • @QPaysTaxes the existing answers is not working firefox browser in ubuntu 16.04 – bob marti Commented Mar 31, 2017 at 8:48
  • please give me a suitable solution – bob marti Commented Apr 18, 2017 at 15:33
  • 1 This question is not working. – anon Commented Apr 18, 2017 at 20:59
  • 1 If that ^ wasn't particularly helpful, maybe now you understand what I mean by "clarify". "not working" isn't a problem description./ – anon Commented Apr 18, 2017 at 20:59
Add a ment  | 

2 Answers 2

Reset to default 8

You cannot—as far as I can tell—change the key bination required to activate an accesskey, but what you're trying to achieve does not require use of the accesskey attribute. You can listen for the keydown event directly.

Add a keydown event listener to the document. In the handler function check to see if the alt and s keys are pressed. If they are—and no other modifier keys are pressed—prevent the default action of the event, then trigger a click event on the submit button.Triggering the click event on the submit button will in turn trigger the submit event listener, whereas triggering the submit event on the form directly may not.

const submit = document.querySelector('#submit')

document.addEventListener('keydown', e => {
  // If the only modifier key is the alt key, and the s key is pressed
  if(!e.metaKey && !e.ctrlKey && !e.shiftKey && e.altKey && e.key === 's') {
    e.preventDefault()  // Prevent the mozilla history menu from opening
    submit.click()      // Trigger the form submission
  }
})
<form action=""><button type="submit" id="submit"><u>S</u>ubmit</button></form>

Notes:
1. Though this works in the current stable version of Firefox, I have not tested other versions
2. This example uses language features introduced in the 2015 version of The ECMAScript Language Specification. These features are not required to achieve the desired effect, but make the code easier to read. If you need to support older browsers, you can use var instead of const, and a standard function instead of the fat arrow function.
3. Unfortunately—due to sandboxing—the Stack Overflow snippet feature does not work for this example, see this example on JSBin instead

If you want deep knowledge about it then try this article has more info: http://kb.mozillazine/Ui.key.contentAccess

If you find that you can't get used to the "in-tab" preferences dialog, there is a preference to return to the old style dialog:

  1. In a new tab, type or paste about:config in the address bar and press Enter. Click the button promising to be careful.

  2. In the search box above the list, type or paste pref and pause while the list is filtered

  3. Double-click the browser.preferences.inContent preference to switch it from true to false

Note: I don't know whether that will be in Firefox forever or is a transitional feature.

本文标签: javascriptChange accesskey modifier keys in FirefoxStack Overflow