javascript - How can I know a 2 last pressed key on a keyboard -
is possible know, 2 last keys pressed on keyboard?
how can it?
need compare keys, , if same - make function.
example, if press enter - first function, if after enter press space- second function. if after enter press ctrl - third function.
so, hink onw way - make 2 var current , previous key value, , make if else if function
:-)
way, how current key value
$('#text').keydown(function(event) { $('#show').text(event.keycode); });
or, better question! (i saw right now)
directly in editor, after press doublespace - jump anothe line in live-preview.
how works? i'm not sure, thinks need same.
thank much!
you start very simple jquery plugin:
(function($) { $.fn.keywatcher = function() { return this.keydown(function(evt){ var $this = $(this); var prevevt = $this.data('prevevt'); $this.trigger('multikeydown',[prevevt,evt]); $this.data('prevevt',evt); }); }; })(jquery);
this raise new event (multikeydown
) whenever keydown
event raised. either provide keydown event current key press, or if there previous 1 provide too.
usage like:
$(document).keywatcher().bind('multikeydown',function(evt,prevkey,currkey){ alert('prevkey = ' + ((prevkey) ? string.fromcharcode(prevkey.keycode) : '(none)')); alert('currkey = ' + string.fromcharcode(currkey.keycode)); });
live example (press keys on lower right hand pane in jsfiddle): http://jsfiddle.net/9vmuy/1/
the event provided in prevkey
, currkey
parameters event handler contain original keydown
events, have full access keycode
, ctrlkey
, shiftkey
properties etc.
Comments
Post a Comment