Can I get the amount of time the key is pressed on the keyboard

I am working on a project in which I have to develop bio passwords based on the style of custom keys. Suppose a user enters a password 20 times, his keystrokes are recorded, for example

holdtime: time for which a particular key is pressed. Digraph Time: The time it takes to press another key.

Suppose the user enters the password "COMPUTER". I need to know the time for which each key is pressed. sort of:

Timeout for the above password

C - 200 ms O - 130 ms M - 150 ms P - 175 ms U - 320 ms T - 230 ms E - 120 ms R - 300 ms

The rational behind this is that each user will have a different wait time. Let's say that an old person enters a password, it will take longer than a student. And it will be unique to a particular person. To make this project, I need to record the time for each key pressed. I would really appreciate if anyone could help me on how to get these times.

Editing from here .. The language is not important, but I would prefer it in C. I like getting the data set more.

+4
source share
5 answers

You mentioned that you would prefer it in C, but since you marked it with Python ... :)

In addition, since you are saying that you are looking for creating a data set, I assume that you will have to invite users to enter arbitrary text, so you will need some kind of interface (graphical or another).

Here is a quick example of using pygame. You can trivially change it to ask users to type specific words, but as it is, it simply allows the user to enter arbitrary text, record the time for all keystrokes, and print each hold and digraph in the order in which the user typed it, when it exits (that is, when the user presses Esc).

As Kibibu noted, showing the user what he is typing in real time, a delay is introduced that can mask real keystrokes, so this code only displays what the user typed when he types "Enter."

Update: Now it calculates the digraph and hold time (excluding Enter in both cases).

Update2: Per Adi query changed from displaying the average to displaying each individual time in order.

import sys from collections import defaultdict from time import time import pygame from pygame.key import name as keyname from pygame.locals import * # Mapping of a key to a list of holdtimes (from which you can average, etc) holdtimes = defaultdict(list) # Mapping of a key pair to a list of digraph times digraphs = defaultdict(list) # Keys which have been pressed down, but not up yet. pending = {} # Last key to be de-pressed, corresponding time). last_key = None # Text that the user has typed so far (one sublist for every Enter pressed) typed_text = [[]] def show_times(): all_text = [k for line in typed_text for k in line] print "Holdtimes:" for key in all_text: print "%s: %.5f" % (key, holdtimes[key].pop(0)) print "Digraphs:" for key1, key2 in zip(all_text, all_text[1:]): print "(%s, %s): %.5f" % (key1, key2, digraphs[(key1, key2)].pop(0)) def time_keypresses(events): global last_key for event in events: if event.type == KEYDOWN: # ESC exits the program if event.key == K_ESCAPE: show_times() sys.exit(0) t = pending[event.key] = time() if last_key is not None: if event.key != K_RETURN: digraphs[(last_key[0], keyname(event.key))].append(t - last_key[1]) last_key = None elif event.type == KEYUP: if event.key == K_RETURN: update_screen() typed_text.append([]) pending.pop(event.key) last_key = None else: t = time() holdtimes[keyname(event.key)].append(t - pending.pop(event.key)) last_key = [keyname(event.key), t] typed_text[-1].append(keyname(event.key)) # Any other event handling you might have would go here... def update_screen(): global screen screen.fill((255, 255, 255)) header_font = pygame.font.Font(None, 42) header = header_font.render("Type away! Press 'Enter' to show.", True, (0, 0, 0)) header_rect = header.get_rect() header_rect.centerx = screen.get_rect().centerx header_rect.centery = screen.get_rect().centery - 100 text_font = pygame.font.Font(None, 32) user_text = text_font.render("".join(typed_text[-1]) if typed_text[-1] else "...", True, (0, 0, 255)) text_rect = user_text.get_rect() text_rect.centerx = screen.get_rect().centerx text_rect.centery = screen.get_rect().centery screen.blit(header, header_rect) screen.blit(user_text, text_rect) pygame.display.update() if __name__ == '__main__': pygame.init() window = pygame.display.set_mode((800, 600)) screen = pygame.display.get_surface() update_screen() while True: time_keypresses(pygame.event.get()) 
+3
source

Record the KeyDown and KeyUp events and make a difference in the timestamps of each of them.

http://code.activestate.com/recipes/203830/

Edit: You can check wxPython, this should help you:

http://www.wxpython.org/onlinedocs.php

in particular:

http://docs.wxwidgets.org/stable/wx_wxkeyevent.html#wxkeyevent

+6
source

Take a look at ncurses . This is a terrific tool for getting information about keystrokes in the terminal.

Check out this link.

+2
source

If you are reading from the terminal in conical mode, you can read each keystroke when pressed. You will not see keydown keyup events as you could if you captured X events, but this is probably easier, especially if you just work in a console or terminal.

0
source

The answer is conditionally yes.

If your languages ​​/ environment supports an interactive keyboard that offers Key-Down and Key-Up events, then you will catch both events and the time difference between them.

This would be trivially easy in JavaScript on a web page, which would also be the easiest way to showcase your work to a wider audience.

0
source

Source: https://habr.com/ru/post/1308193/


All Articles