Keyboard Accessibility: 网页键盘按钮响应
原文地址:http://webaim.org/techniques/keyboard/
Introduction
Ensure that all content can be accessed with the keyboard alone.
Keyboard accessibility is one of the most important aspects of web accessibility. Many users with motor disabilities rely on a keyboard. Blind users also typically use a keyboard for navigation. Some people have tremors which don't allow for fine muscle control. Others have little or no use of their hands. Some people simply do not have hands, whether due to a birth defect, an accident, or amputation. In addition to traditional keyboards, some users may use modified keyboards or other hardware that mimics the functionality of a keyboard.
Potential Problems
There are many ways that a webpage can introduce difficulties for users who rely on a keyboard for navigation. Below are a few of the most common issues.
Focus indicators
A keyboard user typically uses the Tab key to navigate through interactive elements on a web page—links, buttons, fields for inputting text, etc. When an item has keyboard "focus", it can be activated or manipulated with the keyboard. A sighted keyboard user must be provided with avisual indicator of the element that currently has keyboard focus. A basic focus indicator is provided automatically by the web browser and is typically shown as a border (called an outline) around the focused element. However, these outlines can be hidden by applying outline:0 oroutline:none CSS to focusable elements.
Avoid outline:0 or similar styles on links and other elements that can receive keyboard focus.
In addition to the default outline, you can use CSS to make the focus indicator more visually apparent and keyboard-friendly by adding a background color or other visual style to links, and other controls that will receive keyboard focus. The outline can be styled to match your site design.
Items that cannot receive keyboard focus
By default, users can only navigate to links, buttons, and form controls with a keyboard. These natively-accessible elements should be used whenever possible. If this is not possible, you may need to use tabindex="0" to ensure an element can receive keyboard focus.
Note: An <a> element is only keyboard accessible when it has a non-empty href attribute. <a> and <a href=""> should not be used for links.
Inaccessible custom widgets
Although elements that are natively keyboard accessible should be used when possible, there are times when HTML falls short and custom "widgets" are necessary. Complex menus, sliders, dialogs, tab panels, etc. must all be built to support keyboard accessibility. This means the following must occur:
- The interaction is presented in an intuitive and predictable way
- JavaScript event handlers work with a keyboard and a mouse.
- The interaction uses standardized keystrokes.
The ARIA design patterns describe standard keyboard interactions for many common widgets.
Lengthy navigation
Sighted mouse users are able to visually scan a web page and directly click on any item. Keyboard users must press the Tab key or other navigation keys to navigate through the interactive elements that precede the item the user wants to activate. Tabbing through lengthy navigation may be particularly demanding for users with motor disabilities.
Long lists of links or other navigable items may pose a burden for keyboard-only users. The following best practices can facilitate efficient keyboard navigation:
- Provide a "skip to main content" link on the page.
- Use a proper heading structure.
- Provide ARIA landmarks or HTML5 structural elements (
<main>,<nav>, etc.)
Keyboard Testing
Testing with a keyboard is an essential part of any accessibility evaluation. The basics of keyboard testing are simple—the Tab key can be used to navigate through links and form controls, Enter(and sometimes Spacebar) selects an element, and the arrow keys are sometimes used for other navigation. However, when testing with a keyboard, you are not just trying to interact with the page successfully, you also want to ensure all interactions are predictable. This requires an understanding of common keyboard interactions.
The following table includes many of the most common online interactions, the standard keystrokes for the interaction, and additional information on things to consider during testing.
| Interaction | Keystrokes | Notes |
|---|---|---|
| Navigate to most elements |
|
|
| Link | Enter | |
| Button | Enter or Spacebar | Ensure elements with ARIA role="button" can be activated with both key commands. |
| Checkbox | Spacebar - check/uncheck a checkbox | Checkboxes should be used when one or more option can be selected. |
| Radio buttons |
|
Radio buttons should be used when only one option from a group can be selected. |
| Select (dropdown) menu |
|
You can also filter by typing letters, but this behavior varies by browser. Some will filter as you type, like autocomplete. Others will only sort by first letter. E.g., in a list of US States, hitting A then R may take you to Arizona, or it may take you to Alabama and then Rhode Island. |
| Autocomplete |
|
|
| Dialog | Esc - close |
|
| Slider |
|
|
| Menu bar |
|
|
| Tab panel |
|
|
| 'Tree' menu |
|
|
| Scroll |
|
Minimize horizontal scrolling. |
The ARIA authoring practices document provides additional information for these and other common interactions.
浙公网安备 33010602011771号