Skip to content

Instantly share code, notes, and snippets.

@chsh
Created February 26, 2012 02:04
Show Gist options
  • Save chsh/1912270 to your computer and use it in GitHub Desktop.
Save chsh/1912270 to your computer and use it in GitHub Desktop.
Handle all keyevents and react if digit or return key.
class DigKey
constructor: (@catch_elm, @disp_elm, @form_elm) ->
$(@catch_elm).keydown (event) =>
kc = event.keyCode
$(@disp_elm).append dig(kc) if is_num(kc)
if is_cr(kc)
$(@form_elm).submit
else
$(@disp_elm).append '<br/>' if is_cr(kc)
is_num = (key_code) ->
return true if key_code >= 48 && key_code <= 58
is_cr = (key_code) ->
key_code == 13
dig = (key_code) -> (key_code - 48) + ''
window.DigKey = DigKey
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment