Joeytje50 Joeytje50 - 1 year ago 74
HTML Question

Getting the key combination of accesskeys in a browser

I want to make a page where certain inputs and links have accesskeys attached to them, and I want to inform the user of what key combination they need to press to activate the input or link. Is there a way to automatically get the accesskey key combination of a browser via JavaScript, or do I need to detect which browser it is and then just use a table where I store the key combination that browser uses?

I haven't found any scripts that could automatically detect this (I've looked at the source code for Wikipedia, and they also go by browser name), which I find curious, since most sites seem to recommend feature detection as opposed to browser detection. I would find it weird if accesskeys would be the exception to that.

Also, according to Wikipedia, there's a huge load of different key combinations for accesskeys, so I don't think making an array of combinations and picking the browser's combination would be the best solution for that.

Answer Source

Per HTML5, there is a JS attribute called accessKeyLabel that should return a string appropriate to the browser, based on the value of the accesskey attribute.

This may not work across all browsers/versions that you care about, however, in which case you will need a manual browser-sniffing-and-branching fallback. 10–20 such combinations are not a "huge" amount IMHO, and the only reasonable recourse.