Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get info on what key was pressed on for how long?

Imagine this code:

if (navigator.appName == "Opera")
    document.onkeypress = function (e) { console.log(e.keyCode); };
else 
    document.onkeydown = function (e) { console.log(e.keyCode); };

What it does is pretty obvious, I guess. The problem is, if you hold the key for a long period of time, it will be registered a lot of times. In my app, this is a problem because it makes my app do a lot of unnecessary computing. Is it possible to somehow only get a keypress once, but with info on how long the key was held?

like image 378
Aleksejs Popovs Avatar asked Jun 24 '11 19:06

Aleksejs Popovs


People also ask

How do you figure out what key is being pressed?

The Windows on-screen keyboard is a program included in Windows that shows an on-screen keyboard to test modifier keys and other special keys. For example, when pressing the Alt , Ctrl , or Shift key, the On-Screen Keyboard highlights the keys as pressed.

How do you see what key is being pressed Javascript?

Attach an event to the input box. like onkeypress event. Call a function when that event happens and pass the event parameter in it. In the called function, identify the key pressed.


1 Answers

Here you go:

var pressed = {};

window.onkeydown = function(e) {
    if ( pressed[e.which] ) return;
    pressed[e.which] = e.timeStamp;
};

window.onkeyup = function(e) {
    if ( !pressed[e.which] ) return;
    var duration = ( e.timeStamp - pressed[e.which] ) / 1000;
    // Key "e.which" was pressed for "duration" seconds
    pressed[e.which] = 0;
};

Live demo: http://jsfiddle.net/EeXVX/1/show/

(remove the "show/" part of the URL to view the code for the demo)

So you have the pressed object which monitors which keys are currently being pressed and at what point (in time) they have been pressed.

Inside the keyup handler, you determine if the key was being pressed, and if so, calculate the duration by subtracting the time-stamps of the keyup/keydown events.

like image 120
Šime Vidas Avatar answered Sep 22 '22 15:09

Šime Vidas