Mousetrap - javascript library that allows you to easily and easily add hotkeys to sites, appeared not so long ago. But I already managed to catch the fancy of me so much that I decided to make its addition to the Ruby on Rails project simple and enjoyable. Namely, wrap in a ruby library for rails. This is how the gem mousetrap-rails appeared .Gemfile your application's Gemfile . gem 'mousetrap-rails' $ bundle install keybindings.js.coffee and connect the library to Asset Pipeline by adding //= require mousetrap to the js application manifest. $ rails generate mousetrap:install data-keybinding ) out of the box to create page navigation. = link_to 'Homepage', root_path, data: { keybinding: 'h' } allows you to go to the home page by clicking on the 'h'Username input field using the 'u' button = text_field_tag 'Username', nil, data: { keybinding: 'u' } // Mousetrap.bind '4', -> alert '4 pressed!' Mousetrap.bind 'x', (-> alert 'x pressed!'), 'keyup' // Mousetrap.bind 'command+shift+k', -> alert 'command+shift+k pressed!' false Mousetrap.bind ['command+k', 'ctrl+k'], -> alert 'command+k or ctrl+k pressed!' false // Mousetrap.bind 'g i', -> console.log 'gi sequence pressed!' Mousetrap.bind '* a', -> console.log '* a sequence pressed!' // - Mousetrap.bind 'up up down down left right left right ba enter', -> console.log 'You WIN!' Source: https://habr.com/ru/post/152057/
All Articles