Skip to content

Instantly share code, notes, and snippets.

@barneycarroll
Created March 28, 2015 17:42
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save barneycarroll/31c1e305a37999a3473f to your computer and use it in GitHub Desktop.
Save barneycarroll/31c1e305a37999a3473f to your computer and use it in GitHub Desktop.
onkeypress : onKey( 'enter', save ), etc
var onKey = (function(){
var keymap = {
'enter' : 13,
'space' : 31,
'tab' : 9,
'esc' : 27,
'left' : 37,
'up' : 38,
'right' : 39,
'down' : 40
};
return function bind( key, callback ){
if( key in keymap ){
key = keymap[ key ];
}
return function handler( e ){
if( e && key === e.keyCode || key === String.fromCharCode( e.keyCode ) ){
callback.call( this, e );
}
else {
m.redraw.strategy( 'none' );
}
};
};
}());
@StephanHoyer
Copy link

Usage

m('input', {
  onkeypress: onKey('enter', yourkeypresshandler) 
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment