Skip to content

Instantly share code, notes, and snippets.

@samccone
Created April 9, 2014 00:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samccone/10211371 to your computer and use it in GitHub Desktop.
Save samccone/10211371 to your computer and use it in GitHub Desktop.
App.module "Behaviors", (Behaviors, App, Backbone, Marionette) ->
class Behaviors.KeyEvents extends Marionette.Behavior
onShow: ->
$(window).on 'keydown', @checkKey
onClose: ->
$(window).off 'keydown', @checkKey
shouldIgnore: ->
a = document.activeElement.tagName
return if a is "INPUT" or a is "TEXTAREA"
checkKey: (e) =>
return if @shouldIgnore()
if toInvoke = @options[e.keyCode]
fn = toInvoke.call(@view)
if ~@options.preventDefault.indexOf(e.keyCode)
e.preventDefault()
return false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment