Skip to content

Instantly share code, notes, and snippets.

@mahemoff
Created November 6, 2012 04:18
Show Gist options
  • Save mahemoff/4022515 to your computer and use it in GitHub Desktop.
Save mahemoff/4022515 to your computer and use it in GitHub Desktop.
Mousetrap.js support for Bootstrap with help dialog
# Monkey-patch stopCallback so that Mousetrap will ignore any keyboard action during modal
((originalStopCallback) -> Mousetrap.stopCallback = ->
return true if $('body.modal-open').length
originalStopCallback.apply(Mousetrap, arguments)
)(Mousetrap.stopCallback)
# "?" key will produce keyboard shortcuts dialog (as on Twitter and GitHub)
# It fetches the dialog content on demand for performance reasons
Mousetrap.bind '?', ->
$.get '/docs/keyboard_shortcuts', (html) ->
if !$wrapper
$wrapper = $('<div class="keyboard-shortcuts-wrapper">loading &hellip;</div>')
$wrapper.appendTo('body').html(html)
$('.modal', $wrapper).modal('toggle')
# Typical search shortcut
Mousetrap.bind '/', (-> $('.query').focus()), 'keyup'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment