#include <iostream>
#include <chrono>
#include <thread>
#include <conio.h>//not standard.
class StopWatch {
std::chrono::high_resolution_clock::time_point S;
std::chrono::high_resolution_clock::time_point E;
public:
typedef std::chrono::milliseconds TimeType;
StopWatch() { Start(); };
bool Start() {
S = E = std::chrono::high_resolution_clock::now();
return true;
}
bool ReStart() {
return Start();
}
bool Stop() {
E = std::chrono::high_resolution_clock::now();
return true;
}
bool Reset() {
S = E = std::chrono::high_resolution_clock::now();
return true;
}
template<class T = TimeType>
T Ellipse() {
return std::chrono::duration_cast<T>(std::chrono::high_resolution_clock::now() - S);
}
template<class T = TimeType>
T Result() {
return std::chrono::duration_cast<T>(E - S);
}
};
int main() {
int key = -1;
L1:
std::cout << "wait enter key!" << std::endl;
key = _getch();
if (key == 27) {
return 1;
}
std::this_thread::sleep_for(std::chrono::nanoseconds(1));
if (key != '\r') goto L1;
StopWatch SW;
key = -1;
std::cin.clear();
SW.Start();
while (key != '\r') {
key = -1;
std::cout << "wait enter key!" << std::endl;
std::this_thread::sleep_for(std::chrono::nanoseconds(1));
if (_kbhit()) {
key = _getch();
if (key != '\r') {
std::cout << key << "is not enter key!" << std::endl;
}
}
if (key == 27) {
return 1;
}
}
SW.Stop();
std::cout << "Count:" << SW.Result().count()<<"ms." << std::endl;
std::cin.clear();
return 0;
}