Ruby on Rails 键盘快捷键

发布于 2024-08-18 05:18:34 字数 82 浏览 3 评论 0 原文

有谁知道如何在我的网站上使用 Ruby on Rails 设置键盘快捷键?例如,如果用户想使用键盘快捷键与网站交互,而不是单击按钮/链接,我该怎么做?

does anyone know how to set-up keyboard shortcuts using Ruby on Rails on my website? For example if a user want to interact with the site using keyboard shortcuts instead of clicking buttons/links how would I do this?

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

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

发布评论

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

评论(3

半窗疏影 2024-08-25 05:18:34

最简单的方法是为元素的 accesskey 属性设置一个值。如果您想通过 Rails 执行此操作,可以通过向 submit_tag 辅助方法传递一个额外选项来实现此目的,如下所示:

<%= submit_tag("Save and Refresh", :accesskey => "R") %>
// Equivalent to <input type="submit" value="Save and Refresh" accesskey="R" />

这将导致在 Alt< 时“单击”该按钮/kbd>+R(或 Alt+Shift+R,具体取决于您的浏览器)被按下。 accesskey 属性可用于

如果您想要做一些更复杂的事情(例如 GMail 的键盘快捷键),您将必须编写一些 JavaScript。它的核心是一个事件处理程序,用于监视文档上的按键,然后在按下某个键时调用其他 JavaScript 函数来运行您想要的代码。这是一种基于按键设置快捷方式的非常简单的方法(这使用 Prototype,这是 Rails 默认使用的 Javascript 库,并且未经测试):

$(document.body).observe("keypress", function(event)
{
  var keyCode = event.which || event.keyCode; // W3C and Microsoft's event models
                                              // have differing ways of
                                              // determining which key was pressed
  var keyChar = String.fromCharCode(keyCode); // turn a code into an actual character

  switch (keyChar)
  {
    case 'a':
      // Run code if the user presses 'a'
      break;

    // ...
  }
});

这是另一个 SO 问题,涉及 Javascript 中的键盘快捷键。

请注意,这些解决方案都根本不依赖于 Rails。

The simplest way is to set a value for the accesskey attribute for your elements. If you wanted to do this through Rails, you could do this by passing an extra option to the submit_tag helper method, like so:

<%= submit_tag("Save and Refresh", :accesskey => "R") %>
// Equivalent to <input type="submit" value="Save and Refresh" accesskey="R" />

Which will cause that button to be "clicked" when Alt+R (or Alt+Shift+R, depending on your browser) is pressed. The accesskey attribute is available for <input>, <button> and <a> HTML elements.

If you are looking to do something more complex (like GMail's keyboard shortcuts, for example), you will have to write some javascript. The core of it would be an event handler that watches for keypresses on the document, and then calls other javascript functions to run the code that you want when a certain key is pressed. Here's a very simplistic way of setting up shortcuts based on a key press (this uses Prototype, which is the Javascript library that Rails uses by default, and is untested):

$(document.body).observe("keypress", function(event)
{
  var keyCode = event.which || event.keyCode; // W3C and Microsoft's event models
                                              // have differing ways of
                                              // determining which key was pressed
  var keyChar = String.fromCharCode(keyCode); // turn a code into an actual character

  switch (keyChar)
  {
    case 'a':
      // Run code if the user presses 'a'
      break;

    // ...
  }
});

Here is another SO question that deals with keyboard shortcuts in Javascript.

Note that neither of these solutions really rely on Rails at all.

仅冇旳回忆 2024-08-25 05:18:34

查看此页面 关于 Web 应用程序的键盘快捷键。该站点提供了执行此功能的 .js。

Look on this page about keyboard shortcuts to a web application. This site supplies a .js that performs this functionality.

帅气尐潴 2024-08-25 05:18:34

您可以使用任何 JavaScript 库来提供键盘快捷键。由于 Rails 默认使用 Prototype (对于 JavaScript 辅助方法),您可能需要查看 原型热键

该网站中有一些使用示例。

You can use any JavaScript library to provide keyboard shortcuts. Since Rails uses Prototype by default (for the JavaScript helper methods), you might want to check out prototype-hotkeys.

There are some usage examples in that website.

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