View Full Version : the accesskey


spikemeister
01-13-2004, 07:25 PM
This may seem a little trivial to some of you but is there a way to set up accesskeys so that in IE the user doesnt have to press enter after the key?

~x~karyn~x~
01-14-2004, 12:42 AM
Can you rephrase this question? I'm not exactly sure what you are talking about...

spikemeister
01-14-2004, 12:48 AM
Sure, currently when using the accesskey in IE (i have not tried other browsers with it) pressing the defined accesskey only highlights the link and waits for the user to press enter before continuing. I would like the accesskey to be pressed and the link is activated automatically without the need for the user to press enter.

burntsushi
01-14-2004, 02:18 AM
If you're dealing with forms, I believe you have to combine "alt" with the accesskey. So, if the accesskey was "s", then you would hit "alt + s" on your keyboard to activate it. But I've only tested that with forms, but you're welcome to try it with other things ;)

Lemme know if that works out :)

spikemeister
01-14-2004, 02:21 AM
yep sorry i should have said that, altand the accesskey still requires the user to press enter.

burntsushi
01-14-2004, 02:22 AM
hmmmm....

Is there any way to see an example of what you're doing?

The only other thing I can think of, is to set up a javascript, that when the appropriate button is pushed, a certain action will occurr... i don't think it would be too hard ;)

spikemeister
01-14-2004, 02:29 AM
I think that is what ill have to do as there seems no other way round it. Cheers for the help! :)

burntsushi
01-14-2004, 03:01 AM
hey, i'm not done helping yet :)

I cooked this up, and it should solve your problem ;)

put this between the <head> and </head> tags of your document:

<script type="text/javascript">
<!--

function doAccessKey(theEvent) {
// make sure DOM is supported...
if(!document.getElementById) {
return false;
^^

if(theEvent.keyCode) {
keycode = theEvent.keyCode;
^^ else {
keycode = theEvent.which;
^^

keycode = String.fromCharCode(keycode);

// get all A
aObjs = document.getElementsByTagName("a");

// loop...
for(x = 0; x < aObjs.length; x++) {
if(keycode == (aObjs[x].getAttribute("accesskey"))) {
window.location = aObjs[x].getAttribute("href");
^^
^^
^^

-->
</script>

Put what's bolded into your body tag:

<body onkeypress="doAccessKey(event);">

And this is what a link will look like:

<a href="someplace.html" accesskey="s">Click</a>

This will only work with a tags, that have the href attribute. Of course, you could modify the script to work with any tag really...

Here's the breakdown:
The script automatically goes though every a tag in your document, whenever a key is pressed. It then grabs the value of that specific accesskey (as specified IN the <a> tag) and compares it with the one that was pressed. If they're equal, it will automatically execute the action (in this case, go to the page that link is linking too). The script automatically grabs the link from the href attribute, so less typing for you :)

Hope my little handy dandy script helps you :D

you must replace all "^^" with the RIGHT curly brace!!!!!

spikemeister
01-14-2004, 11:16 AM
Thanks very much you just saved me a job! :)

bellportal
01-14-2004, 04:20 PM
I didn't know you could do this - is there anything on Lissa's site about it?

spikemeister
01-14-2004, 04:23 PM
I have to say i never checked the rest of lissas site to find out. I consider the accesskey to be quite important when it comes to making pages now, not only because of accessibilty but because i like the fact that users can quickly navigate my sites using key presses only. :)

burntsushi
01-14-2004, 10:05 PM
Ironically, I'd never implement that javascript into my web sites... here's why:

1. How will the user know which key to press?

2. What if they accidentally hit a key..? the next thing they know, they're on a different page.

3. What if you have a form on your page?

I'd only use accesskey's the proper way- by just letting it put focus on the link, or when using forms, submitting the form.

I'm just really critical though.. I must say, it is something very cool to spice up your site, just isn't my style :)

bellportal
01-15-2004, 08:28 AM
Ironically, I'd never implement that javascript into my web sites... here's why:

1. How will the user know which key to press?

2. What if they accidentally hit a key..? the next thing they know, they're on a different page.

3. What if you have a form on your page?

I'd only use accesskey's the proper way- by just letting it put focus on the link, or when using forms, submitting the form.

I'm just really critical though.. I must say, it is something very cool to spice up your site, just isn't my style :)
I would agree with some of that - but not all! ;)

spikemeister
01-15-2004, 08:31 AM
Your right burntsushi i think ill leave it as it is. :)