Many developers who are aware of the WAI and checkpoint 9.5 ‘Provide keyboard shortcuts to important links’, have recently avoided the use of accesskeys due to inherent problems. John Foliot, along with Derek Featherstone, of Wats.ca have long been advocates of not using them because of this.
Following on from a recent discussion, Gez Lemon of Juicy Studio wrote a quick and dirty php script that would allow a user to set their own accesskeys for a site. He gave me permission to take that script and to hopefully turn it into a usable and functional set of scripts.
I have long thought that one of the problems with accesskeys is the inconsistent key mapping from site to site – thus creating a minefield of accesskeys in use, with no possibility of anyone remembering them all. The idea being that the user gets to say what is, or isn’t used and can create their own set of accesskeys that can re-used on any site that offers this facility. No more having to remember a myriad of different accesskeys.
As such, I have just completed a set of php which will hopefully do just this. However, if anyone spots any errors, or has feedback, then please let me know and the scripts will be amended accordingly.
Update: as of 20th December this entry has been superceded by Access Keys revisted, comments on this article are therefore now closed.
For those want to dive straight in, the working demo is available on Quirm.net. The AccessKeyClass.zip contains all the scripts utlised in the demo.
The Scripts
functions.php
This only containes one function, but purely for the demo I have also included an array of some example links where we might want to allow users to set an accesskey.
$links = array(
'Home'=>'index.php',
'Contact Us'=>'/contact/',
'Accesskeys'=>'akeyset.php'
);
Obviously, you will need to populate this array in line with your intended usage on your site. The format is name=>path.
function akeyset($link,$linkpath){
$linkname=str_replace(' ','',$link);
if (isset($_COOKIE[$linkname]) && trim($_COOKIE[$linkname]) != ""){
echo '<li><a href="'.$linkpath.'" accesskey="'.$_COOKIE[$linkname].'">'.$link.'</a>[Key: '.$_COOKIE[$linkname].']>/li>';
}else{
echo '<li><a href="'.$linkpath.'">'.$link.'</a></li>';
}
}
The akeyset function checks whether a link has an accesskey assigned, and will display it if set. It can be called within the appropriate php page using:
akeyset('Home','index.php');
akeyset.php
This script will enable users to define their own accesskeys to links on your site, providing you have chosen to incorporate the appropriate link within the $links array.
The main code for setting up the accesskeys, including error checking. can be found in the accesskey.zip.
definekeys.php
The cookie setting and unsetting are handled in this script.
require ('functions.php');
$strYear = time()+60*60*24*365;
$strPast = time()-60*60*24*365;
//unset all values in the cookie
foreach(array_keys($links) as $lname ){
$lname=str_replace(' ','',$lname);
setcookie($lname, '', $strPast,'/');
}
//set new cookie values only
foreach($_GET as $gname => $gkey ){
$gname=str_replace(' ','',$gname);
setcookie($gname, $gkey, $strYear,'/');
}
//return to the settings page, the accesskeys will show up straight away
header("Location: akeyset.php");
Initially, the list of possible accesskeys are parsed from the $links array, and any associated pre-existing cookies are deleted. A new cookie is then created for each new accesskey. Utilising a separate script for the cookie setting ensures that the new accesskeys are immediately available to the user.
akbinding.js
This is a javascript file that enhances this utility, but is not required for it to work, and was written by Gez Lemon. (Besides which my javascript skills sucks!) You’ll have to ask Gez what it does!
Edit: Gez has contacted me, apparently the javascript needs updating to match the php. I will attempt this, but it may be a while before it is completed!
My thanks go to Gez Lemon for coding the inital scripts.
Edit: As soon as I have a chance I’ll release version 2 – as a php class – which will hopefully be more portable.
Update: as of 20th December this entry has been superceded by Access Keys revisted, comments on this article are therefore now closed.
Pingback: Spider Trax » Access Keys - A Server-Side Solution!
I know this is *not* as elegante as Gez’s solution and may pollute the markup a little, but what about a “quick and dirty” approach?
Using ASP I would simply plug session variables as values of the accesskey attribute.
Something like:
<a href=”/” accesskey=”<%= Session(‘AccessKeyForHomePage’) %>” title=”Accesskey: <%= Session(‘AccessKeyForHomePage’) %>”>Home</a>
Using sessions are perhaps a useful backup, but would require the user to set up the accesskeys for each visit to the site, which is one thing I/we were trying to avoid.
In this case, I’d use Response.Cookies() with Response.Cookies().Expires and then use Request.Cookies() instead of Session()
I’m not saying it’s “pretty” though 🙂
Pingback: Blogging the Meme | Elysian Systems
I thought it would be nice to have an easier way to unset the accesskeys, so I adjusted the script a little to provide an additional form if any accesskeys were set.
You can see the changes at http://dev.joedolson.com/akeys/.
(The forms are NOT nested – I’m not sure why they look like they are.)
Involved some small changes to akeyset.php and definekeys.php – changed files available at http://dev.joedolson.com/akeys/changes.zip, if you want to see my crude solution.
Okay, I found the problem . . . very silly error. changes.zip is updated to reflect the change. (Surprised that the validator didn’t catch it!)
Good idea Joe. As soon as I get chance i want to turn all this into a nice useable class, and I’ll make sure that that is in there as an option.
Joe,
A separate form isn’t required to unset access keys. Unchecking the checkbox removes the access key binding.
I know – the only difference here is that you can unset all keys with a single action. Saves a little effort for the mobility impaired!
Well done Rich and Gez! It will be good (on appropriate sites) to move the user access keys to a database along with any other user preferences (alternative stylesheet, etc.) I use Text-DB-API quite a bit (http://www.c-worker.ch/txtdbapi/index_eng.php) and I’m starting to imagine a small, flat-file-database-driven accessibility preferences application… Thanks again for the work and ideas 🙂