-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeyboardFeatureExtraction.py
More file actions
97 lines (74 loc) · 3.32 KB
/
Copy pathkeyboardFeatureExtraction.py
File metadata and controls
97 lines (74 loc) · 3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
from cProfile import label
from tkinter import Y
from pynput import keyboard
import numpy as np
from time import time
from database import DataBaseConnector
from PyQt6 import QtCore, QtGui, QtWidgets
class keyboardFeatureExtraction(object):
def __init__(self) -> None:
self.dwell = [] #The list of key dwell times
self.startTimes = np.zeros(254) #Saves the time in witch a key was pressed
self.startTyping = 0 #strat time
self.DownDown = []
self.UpDown = []
self.lastKeyEnterdTime = 0 #a variable used to caclulate the duration between key strokes
self.virtualKeysID = [] #Contains the key char that was stroked for exsample if k was pressed 'k' will be added
self.qui = None
self.charCount = 150
def setQui(self,qui):
self.qui = qui
def setEmail(self,email):
self.email = email
def on_press(self,key):
currTime = time()
if self.startTyping == 0:
self.startTyping = currTime
if self.lastKeyEnterdTime != 0:
if hasattr(key, 'vk'):
if self.startTimes[key.vk] == 0:
self.DownDown.append(currTime - self.lastKeyEnterdTime)
elif self.startTimes[key.value.vk] == 0:
self.DownDown.append(currTime - self.lastKeyEnterdTime)
self.lastKeyEnterdTime = currTime
if hasattr(key, 'vk'):
if self.startTimes[key.vk] == 0:
self.startTimes[key.vk] = currTime
self.virtualKeysID.append(key.vk)
else:
if self.startTimes[key.value.vk] == 0:
self.startTimes[key.value.vk] = currTime
self.virtualKeysID.append(key.value.vk)
print(key,end='')
def on_release(self, key):
currTime = time()
if self.charCount > 0 and self.charCount <= 150:
self.charCount -= 1
if hasattr(key, 'vk'):
start = self.startTimes[key.vk]
self.startTimes[key.vk] = 0
else:
start = self.startTimes[key.value.vk]
self.startTimes[key.value.vk] = 0
self.dwell.append(currTime - start)
if key == keyboard.Key.backspace:
if len(self.qui.Q1TextInputCube.toPlainText()) > 150:
self.charCount = 0
else:
self.charCount = 150 - len(self.qui.Q1TextInputCube.toPlainText())
if key == keyboard.Key.esc:
# Stop listener
return False
self.qui.setCountLable(self.charCount)
def preProcessing(self):
dwell = np.array(self.dwell)
DownDown = np.array(self.DownDown)
UpDown = DownDown - dwell[:len(dwell)-1]
i = 0
finalVec = []
for i in range(len(dwell)-1):
inputVector = (self.virtualKeysID[i],self.virtualKeysID[i+1],dwell[i],dwell[i+1],DownDown[i],UpDown[i])
finalVec.append(inputVector)
DataBaseConnector._instance.insertInputData(email=self.email, input=str(finalVec))
#print('finalVec - ',end='\n\n')
#print(finalVec)