使用材料UI的React.js应用程序的键盘快捷键?

发布于 2025-02-03 16:24:02 字数 108 浏览 2 评论 0原文

我想在React.js项目中实现键盘快捷键,我正在使用材料UI组件。

浏览器支持的默认键是选项卡,移位,输入和箭头键。

我想实现功能键,以导航到我的Web应用程序的特定区域。

I want to implement a keyboard shortcut in react.js project i am using material UI components.

The default keys which browser supports are tab, shift, enter and arrow keys.

I want to implement function keys to navigate into specific area of my web application.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浪漫之都 2025-02-10 16:24:02

首先,请确保您在区域中使用正确的HTML元素,例如< main>< ainge> or 或< footer> ,并且他们通过aria-labelaria-labelleddy携带可访问的名称。页面上某个区域的咏叹调角色是地标:

地标是一部分内容的抽象角色,它很重要,用户可能希望能够轻松地导航到该部分,并将其包含在页面的动态生成的摘要中。地标允许辅助技术导航并快速找到内容。

> aria> aria:landmark在mdn上的角色

aria> aria 然后

,您可以考虑使用 skip links ,允许键盘用户直接通过这些地标导航到这些地标 tab 的of。这也可能使您有机会传达您提供的捷径。

快捷方式

在提供快捷方式时,重要的是不使用用户平台上已经使用的快捷方式。这些仍应按预期工作。

另外,如果您使用的是单个角色快捷方式,则需要提供一种机制来更改这些快捷方式,如果它们在快捷方式中干扰其他捷径:

理解成功标准2.1.4:字符密钥快捷方式

实现快捷方式的一种方法是本机accesskey 属性,但是浏览器和浏览器和浏览器和在支持下不是很好。

webaim上的accessKey

通常,这些键通常用于交互式元素,而不是用于地标。因此,如果您想集中地标,则需要首先将其集中

<main id="main" tabindex="-1">

keydown事件,然后依次将相应的地标聚焦。

document.documentElement.addEventListener('keydown', e => {
  switch(e.key) {
    case "F1":
      document.getElementsByTagName('main')[0].focus();
      break;
  }
})

由于这仅在DOM上起作用,因此您需要将此处理程序绑定到词根,而不是纪录片,或者呈现应用程序的位置。

Web应用程序和选项卡

如果您在应用程序中实现了组件的箭头密钥导航,则用户可以通过 tab 来达到地标/组件,并实现它可能很快跳过链接或快捷方式可能不太有趣。

First, make sure that you are using the right HTML elements for your areas, like <main> or <region> or <footer>, and that they carry an accessible name by means of aria-label or aria-labelledby. The ARIA role for an area on a page is landmark:

A landmark is an abstract role for a section of content that is important enough that users will likely want to be able to navigate to the section easily and have it included in a dynamically generated summary of the page. Landmarks allow assistive technologies to navigate and to find content quickly.

ARIA: landmark role on MDN

Use Skip Links

Then, you might consider using Skip Links that allow keyboard users to navigate to these landmarks directly by means of tab. This might also give you an opportunity to communicate the Shortcuts you are providing.

Shortcuts

When providing shortcuts, it’s important to not use shortcuts that are already in use on the user’s platform. These should still work as expected.

Also, if you are using single character shortcuts, you need to provide a mechanism to change these shortcuts, should they interfere with other AT shortcuts:

Understanding Success Criterion 2.1.4: Character Key Shortcuts

One way to implement shortcuts is the native accesskey attribute, but browser and AT support is not great.

Accesskey on WebAIM

Usually, these keys are used for interactive elements, not for landmarks. So if you want to focus a landmark, you will need to make it focusable first:

<main id="main" tabindex="-1">

Then you can bind on the keydown event and focus the corresponding landmark in turn.

document.documentElement.addEventListener('keydown', e => {
  switch(e.key) {
    case "F1":
      document.getElementsByTagName('main')[0].focus();
      break;
  }
})

As this only works on the DOM, you will need to bind this handler either to your root instead of the documentElement, or where you render your application.

Web Applications and tab

If you implemented arrow key navigation for components in your application, it might be pretty fast for users to reach a landmark/component by means of tab, and implementing skip links or shortcuts might be less interesting.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文