I posted a server-side solution to allow users to define their own access keys using a PHP class. This is a classic ASP version of the same class, including an example of how to use the class.

Author: Gez Lemon


The AccessKey Class

The ASP AccessKey class allows users to define their own access keys. Access keys are a contentious area of accessibility, as the way they have been implemented in some user agents mean that they clash with the shortcut keys of the browser. The class provides various methods to help developers provide their own customisation form, along with methods to use them in their markup. For a PHP version of this class, please see the PHP version of this article.

The file contains the class and a sample file illustrating how to use the class. The following is an example of the markup to create the user-defined access key form.

<% Option Explicit %>
<!-- #include virtual="/classes/ak.class.asp" -->
Dim objAK

Set objAK = New AccessKeys

objAK.addAccessitem "Home", _
                    "/index.asp", _
objAK.addAccessitem "Search Criteria", _
                    "/contact.asp", _
objAK.addAccessitem "Quality Assurance", _
                    "/services.asp", _
objAK.addAccessitem "Articles Archive", _
                    "/articles.asp", _

If Request.Form("submit") <> "" Or _
   Request.Form("default") <> "" Then
    If Request.Form("default") <> "" Then
    End If
End If
    "-//W3C//DTD HTML 4.01//EN"
<html lang="en-gb">
  <title>User-Defined Access Keys</title>
  <meta http-equiv="Content-Type"
        content="text/html; charset=ISO-8859-1">
<h1>User-Defined Access Keys</h1>
objAK.displayOptions "nav"

If Not objAK.isValid Then
    Response.Write objAK.getErrors
End If

<form id="accesskeysetup" action="ak.asp" method="post">
<% objAK.generateForm False %>
<input type="submit" name="submit"
       value="Assign Accesskey Bindings">
<input type="submit" name="default"
       value="Assign Default Bindings">
Set objAK = Nothing

AccessKey Methods

The following lists the methods that can be used with the AccessKeys Class.

addAccessitem Method

The addAccessitem method allows a single item to be added to the collection of items that may be assigned an accesskey.


objAK.addAccessitem "Meat", "meat.asp", Null
objAK.addAccessitem "Fish", "fish.asp, "9"

removeAccessItem Method

The removeAccessitem method removes an item from the collection of items that may be assigned an accesskey.


objAK.removeAccessitem "Meat"

isValid Method

The isValid method returns a Boolean value indicating whether or not the values assigned were valid.


If Not objAK.isValid Then
    Response.Write objAK.getErrors
End If

getErrors Method

The getErrors method returns a string containing errors from form submissions, or an empty string if there are no errors. The getErrors method should be used with the isValid method.


If Not objAK.isValid Then
    Response.Write objAK.getErrors
End If

setCookies Method

The setCookies method returns a Boolean value indicating whether or not the values were valid accesskey values. If the values are inappropriate, isValid returns False, and getErrors can be used to retrieve the errors.



removeAllBindings Method

The removeAllBindings method removes all access keys defined for the collection.



displayOptions Method

The displayOptions method displays a definition list where the definition terms are the names in the collection, and the definitions contain the assigned key, and suggested key if available.


Display options without specifying an id.

objAK.displayOptions Null

Display options with an id.

objAK.displayOptions "someid"

generateForm Method

The generateForm method generates form sections based on the names provided in the collection. The method takes a parameter to indicate whether the generated markup is XHTML.


The following generates the form markup for HTML:

objAK.generateForm False

The next example generates the form markup for XHTML:

objAK.generateForm True

useDefaults Method

The useDefaults method assigns all of the values that have been suggested as default keys by the author, if any have been provided.



getAccessitem Method

The getAccessitem method returns the markup for a particular item, including the accesskey if one has been defined. If the item doesn't exist in the collection, the method returns an empty string. The accesskey can be ignored using the bKey parameter, in which case, just the markup will be returned. As access keys may be assigned to links, labels, or form controls, a parameter allows the markup to return label tags or link tags.


An example of how this might be used in a navigation list

<ul id="navigation">
  <%= objAK.getAccessitem "Home", True, "link" %>
  <%= objAK.getAccessitem "Contact Us", True, "link" %>

An example of how this might be used with a form element.

<%= objAK.getAccessitem "Search", True, "label" %>
<input type="text" id="search" name="search" size="10">

The following retrieves a link without the access key assignment.

<%= objAK.getAccessitem "Contact Us", False, "link" %>

Category: Accessibility.


  1. [user-defined-access-keys-aspversion.php#comment2]

    Thank you, Thierry, and thank you for posting your version. I would like to see accesskeys used more, and empowering users by allowing them to set their own definitely seems to be the right approach to me.

    Nice work.

    Posted by Gez on

Comments are closed for this entry.