-9

I need to create a features: when a user held pressed a key, a function A is called. When a user release the button function B is called.

I need to make sure that first function runs only when the button is HELD. At the moment using chrome, when I keep pressed a button the function A is called repeatedly, and function B is running when release.

How is possible in java-script or jquery?

GibboK
  • 71,848
  • 143
  • 435
  • 658
  • 3
    [keyup](https://developer.mozilla.org/en-US/docs/Web/Events/keyup) and [keydown](https://developer.mozilla.org/en-US/docs/Web/Events/keydown) event – Satpal Aug 19 '14 at 07:52
  • You can write function for `keypress` and `keyup` events in jquery. – Jenz Aug 19 '14 at 07:54
  • basically I need to develop something like show hide password as in the login windows in windows 8 – GibboK Aug 19 '14 at 07:55
  • keydown and keypress....also in market – A.T. Aug 19 '14 at 07:55
  • so what is the difference between key down and keypress? – GibboK Aug 19 '14 at 07:56
  • 4
    @GibboK http://stackoverflow.com/questions/1367700/whats-the-difference-between-keydown-and-keypress-in-net. Also not really sure why you're asking this type of question if you know how SO works (which you evidently do from your rep/badges). The answer is a quick google away/in the related answers on SO. – SubjectCurio Aug 19 '14 at 07:58

3 Answers3

2
$('#element').keyup(function() {
});

or

$('#element').bind('keyup', function() {
});
Laurens Kling
  • 2,221
  • 1
  • 21
  • 31
0

jQuery's keyup should be what you're looking for: http://api.jquery.com/keyup/

You can bind it to input fields or the entire window, the latter probably being what you're looking for.

Eugen Timm
  • 744
  • 6
  • 13
0

If your app should react in every part of html page use below code:

$(window).keydown(function(event){
    alert(event.keyCode);
});
$(window).keyup(function(event){
    alert(event.keyCode);
});

If only part of the window (like some div) use proper jquery expression like $("#myElement").keydown

Piotr Stapp
  • 19,392
  • 11
  • 68
  • 116