What would be the time when pressed?
$(captureInput).keydown(function(event){
startDOWNPress = (new Date()).getTime();
.
. //Codigo no util para el ejemplo
.
$(captureInput).keypress(function(event){
startPressUP = (new Date()).getTime();
.
.//Codigo no util para el ejemplo
.
$(captureInput).keyup(function(event){
startUPDOWN= (new Date()).getTime();
What I am doing is obtaining the time of tightening, for that I obtain the time from when it is tightened ( keyDown
) until it is initial raised ( keyup
).
How do I do the highlight time?
Enhancement time is from the start of the release until the end of the release.
edit:
Pressure time: The time from when a key is detected as being pressed (KeyDown) until it is detected that it is not pressed any more (KeyUp).
Highlight time: The time from the last time a key is considered to have been entered (KeyPress) until it is detected that it is no longer pressed (KeyUp).
It is not possible to detect these times using only software.
A keyboard is a device that converts a linear magnitude (the height of the key) into a binary magnitude (the electrical circuit of the key is open or closed).
The keys have a force threshold below which the keystroke is not detected, and above it is. "Professional" (read: expensive) keyboards publish this information as part of their specs, and measure it in grams of pressure (or Newton-centimeters). A typical key operates between 30 and 80 grams of pressure.
When you start pressing a key, you apply zero pressure. After a certain threshold, the key begins its journey , and its height begins to drop. As the milliseconds pass and you apply more force, the key passes the detection threshold, an electrical circuit is closed, and a signal is sent to the computer. But the key continues its journey until it physically hits the bottom of the keyboard.
(And vice versa: as you release pressure, the key backs up past the threshold again, the circuit opens, the key continues to travel until you finally apply zero pressure and lift your finger)
I'm going to copy-paste some graphics from https://deskthority.net/wiki/Force :
A key press is a continuous event in time (duration) and space (key height), but the computer only has a binary view of whether the key is pressed or not .
In the graphs you can see the height of the key as more pressure is applied to it, and the black and white dots determine the thresholds at which the computer interprets that key as being pressed.
When that state changes , the software acts accordingly and fires events
keydown
andkeyup
accordingly. The eventskeypress
are different, as they depend on concepts such as character maps and combination keys.If the question was...
...or vice versa...
...then the answer is: you can't .
(Update after reading comments)
One may wonder...
Because the
keydown
andkeyup
contain information relative to keys , while thekeypress
contain information relative to characters .For example: When I press the little finger key of my right hand,
keydown
and are fired ,keyup
reporting ankeycode
orscancode
.47
In addition, a is fired
keypress
, reporting a characterñ
.If you were on a computer with a keyboard set to English, a
keydown
de would be generated47
, identical, but akeypress
different , reporting a character;
. (Idem for dvorak, cyrillic, etc etc keyboards)If I press a modifier key (such as Shift, Ctrl, Altor Bloq May) or a dead key (such as diacritics ´ , `or ¨in Spanish or °, ~or ^in other languages), then the
keydown
and firekeyup
but notkeypress
.For example: if you type
á
pressing ´and then A, they fire:keydown
with keycode 48keyup
with keycode 48keydown
with keycode 38keypress
with personalityá
keyup
with keycode 38One can also ask...
Because the keyboard event standard for the DOM specifies that events should be fired after the operating system (and system libraries, etc etc) have done the mapping of keycodes to characters, and because implementations are a bloody mess and every browser web does whatever the fuck comes out of it .
One can also ask...
Because you are not measuring the time in which the keys are pressed, but you are measuring the time in which you are handling each event that has been fired.
If you're programming in JavaScript, you're in an asynchronous but not concurrent environment , so events are handled one after another, but without you knowing when, and there will always be a time difference when the browser changes the scope of the queries. variables, make new function calls, the OS changes the context of processes, etc etc etc.
Your code doesn't run instantly. The browser must do things and perform calculations and run other event handlers from when a is fired
keydown
to when akeypress
.The goal you are looking for:
Can not be obtained. This is well explained in IvanSanchez's answer .
Instead, we can compute the following values:
In short, they are not real values , but like all measurements, they can be used to obtain behavioral patterns .
Considerations
A key could be held down for a long period, firing successives
keypress
during the same key press. In this case, we will only consider the boost time since the lastkeypress
.However, on MacOS intermediate KeyDown events will not be fired. Linux can behave like Windows or Mac, depending on the platform.
Special keys ( Shift, Ctrl, Inicio, etc.) do not generate events
keypress
. In this case, we can only force an ad-hoc (non-precise) estimate like:[tiempo total de presión] / 2
This calculation is wrong, but we do not have a real event to obtain it. And in these cases, we would be considering equal pressure and enhancement times.
The time when each key was started and finished must be saved independently, since the user could press more than one simultaneously. Example:
When a key is pressed normally, the event
keypress
is usually fired almost immediately afterkeydown
, so the pressure becomes almost insignificant, and the highlight will actually be taking values very close to the total press time . Also, when a key is held down during successive character inputs (aaaaa..
), the eventkeypress
will be very close tokeyup
, so the relationship is reversed (pressure close to full and highlight negligible).The delay with which each of the events can be triggered also depends on the system load, or the calculation that is being carried out after each event. To reduce the degree of error, it is recommended to try to perform the least amount of processing during typing, postponing the analysis or any other task that consumes resources for a stage after data acquisition.
For processing that cannot be postponed to a later stage, it is recommended to try to homogenize, trying to perform exactly the same calculation regardless of the key pressed.
These are the limitations of JavaScript. Events are fired according to values that depend on many factors, and are often far from the actual moment the keys are pressed. If you wanted to get values with higher precision, you could opt for a lower-level language that doesn't run in a browser. These values are not exact , but can be used, with their own inaccuracy, to estimate patterns in typing dynamics .
The keyboards used by common users do not send events at the moment a key is started or stopped, nor at the moment it reaches the maximum of its travel. The KeyDown, KeyPress, and KeyUp events are just interpretations of what the software interprets from key input.
Also, solutions that work around KeyDown, KeyPress, and KeyUp events are geared towards physical keyboards, and have serious compatibility issues on mobile devices. In many cases, without sending the key codes .
Most reliable times
As just noted, JavaScript is very limited in getting the actual times a key is pressed or released. However, we can consider another variable with a lower degree of uncertainty, in which the measurement error tends not to be significant: the interkey time . This variable can be used more reliably to determine typing patterns.
That is, the time that the user takes between when they stop pressing a key Aand when they start pressing a key Bis usually a better indicator in these cases. In addition, it allows us to increase the casuistry, taking into account that the time A⟶ Bwill not be the same as the time A⟶ C.
So, let's also measure the interkey times:
E⟶ j⟶ e⟶ m⟶ p⟶ l⟶o
where you would expect to have significantly different times for any pair of keys.
Code
I came to the conclusion that the following code works. use jquery and pressure times are almost always 0, is it because of the jquery library?