Summary

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

Contents

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 akasp.zip 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", _
                    "1"
objAK.addAccessitem "Search Criteria", _
                    "/contact.asp", _
                    "4"
objAK.addAccessitem "Quality Assurance", _
                    "/services.asp", _
                    Null
objAK.addAccessitem "Articles Archive", _
                    "/articles.asp", _
                    Null

If Request.Form("submit") <> "" Or _
   Request.Form("default") <> "" Then
    If Request.Form("default") <> "" Then
        objAK.useDefaults
    Else
        objAK.setCookies
    End If
End If
%>
<!DOCTYPE HTML PUBLIC 
    "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en-gb">
<head>
  <title>User-Defined Access Keys</title>
  <meta http-equiv="Content-Type"
        content="text/html; charset=ISO-8859-1">
</head>
<body>
<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 %>
<p>
<input type="submit" name="submit"
       value="Assign Accesskey Bindings">
<input type="submit" name="default"
       value="Assign Default Bindings">
</p>
</form>
</body>
</html>
<%
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.

Examples

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.

Example

objAK.removeAccessitem "Meat"

isValid Method

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

Example

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.

Example

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.

Example

objAK.setCookies

removeAllBindings Method

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

Example

objAK.removeAllBindings

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.

Examples

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.

Examples

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.

Example

objAK.useDefaults

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.

Examples

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

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

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.

Comments

  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.