-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKey.h
50 lines (46 loc) · 906 Bytes
/
Key.h
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
#pragma once
#include <iostream>
#include <Windows.h>
#include "Game.h"
using namespace std;
class KeyEvent {
private:
HANDLE hIn;
COORD KeyWhere;
DWORD EventCount;
INPUT_RECORD InRec;
DWORD NumRead;
int downKey;
Game* game;
static unsigned int __stdcall run(void *arg) {
KeyEvent *key = (KeyEvent *)arg;
while (1) {
int temp = key->getKey();
if (temp > 36 && temp < 41)
key->game->setDirection(temp);
}
return 0;
}
public:
KeyEvent() {
hIn = GetStdHandle(STD_INPUT_HANDLE);
EventCount = 1;
}
int getKey() {
ReadConsoleInput(hIn, &InRec, 1, &NumRead);
if (InRec.EventType == KEY_EVENT) {
if (InRec.Event.KeyEvent.bKeyDown) {
downKey = InRec.Event.KeyEvent.wVirtualKeyCode;
return downKey;
}
else {
return -1;
}
}
return -1;
}
void start(Game *game) {
this->game = game;
(_beginthreadex(NULL, 0, run, (void*)this, 0, NULL));
}
};