-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeystroke.cpp
More file actions
73 lines (53 loc) · 2.67 KB
/
Copy pathKeystroke.cpp
File metadata and controls
73 lines (53 loc) · 2.67 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
#include <iostream>
#include <chrono>
#include <map>
#include <vector>
#include <SFML/Graphics.hpp>
// Maps to store vectors of press and release durations for each key
std::map<sf::Keyboard::Key, std::vector<long long>> keyPressDurations; // Press duration (time key held down)
std::map<sf::Keyboard::Key, std::vector<long long>> keyReleaseDurations; // Release duration (time between release and next press)
std::map<sf::Keyboard::Key, std::vector<std::chrono::steady_clock::time_point>> keyPressTimes; // To track press time
auto lastReleaseTime = std::chrono::steady_clock::now(); // Used to track release times
// Vectors to store all press and release durations
std::vector<long long> combinedDurations;
// Function to handle key press events
void keyPress(sf::Keyboard::Key key) {
auto pressTime = std::chrono::steady_clock::now();
keyPressTimes[key].push_back(pressTime); // Store the press time in a vector
}
// Function to handle key release events
void keyRelease(sf::Keyboard::Key key) {
auto releaseTime = std::chrono::steady_clock::now();
if (keyPressTimes[key].empty()) {
return;
}
auto pressTime = keyPressTimes[key].back(); // Get the last press time for this key
keyPressTimes[key].pop_back(); // Remove the last press time since it's been handled
// Calculate press duration (in milliseconds)
long long pressDuration = std::chrono::duration_cast<std::chrono::milliseconds>(releaseTime - pressTime).count();
keyPressDurations[key].push_back(pressDuration); // Store the press duration
// Calculate release duration (in milliseconds)
long long releaseDuration = std::chrono::duration_cast<std::chrono::milliseconds>(releaseTime - lastReleaseTime).count();
keyReleaseDurations[key].push_back(releaseDuration); // Store the release duration
// Update the last release time
lastReleaseTime = releaseTime;
}
// Function to print out the values stored in pressDurations and releaseDurations vectors
void printVectorDurations() {
std::cout << "\n Durations:" << std::endl;
for (const auto& duration : combinedDurations) {
std::cout << duration << " ms ";
}
std::cout << std::endl;
}
std::vector<long long> combineAndreturn(){
for (const auto& entry : keyPressDurations) {
combinedDurations.insert(combinedDurations.end(), entry.second.begin(), entry.second.end());
}
// Extract values from keyReleaseDurations map and store in allReleaseDurations
for (const auto& entry : keyReleaseDurations) {
combinedDurations.insert(combinedDurations.end(), entry.second.begin(), entry.second.end());
}
printVectorDurations();
return combinedDurations;
}