Repository navigation
Expand file tree
/
Copy pathKeyMonitor.h
More file actions
104 lines (101 loc) · 2.6 KB
/
Copy pathKeyMonitor.h
File metadata and controls
104 lines (101 loc) · 2.6 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
98
99
100
101
102
103
104
#ifndef _KEYMONITOR_H
#define _KEYMONITOR_H
#include "GlobalVar.h"
#include <stdio.h>
#if defined(_WIN16) || defined(_WIN32) || defined(_WIN64)
#include <conio.h>
#include <handleapi.h>
#include <processthreadsapi.h>
#include <windows.h>
#define KeyMonitor_Starter() \
HANDLE hThread1 = CreateThread(NULL, 0, KeyMonitor, NULL, 0, NULL)
#define KeyMonitor_Stoper() CloseHandle(hThread1);
#elif defined(__linux__) || defined(__gnu_linux__)
#include <pthread.h>
#include <unistd.h>
#define KeyMonitor_Starter() \
system("stty -icanon"); \
pthread_attr_t attr; \
pthread_t tid; \
pthread_attr_init(&attr); \
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); \
pthread_create(&tid, &attr, KeyMonitor, NULL);
#define KeyMonitor_Stoper() pthread_join(tid, NULL);
#elif defined(__APPLE__)
#endif
// KeyMonitor Function(Different Platform return value type is different)
#if defined(_WIN16) || defined(_WIN32) || defined(_WIN64)
DWORD WINAPI
#elif defined(__linux__) || defined(__gnu_linux__)
void *
#elif defined(__APPLE__)
#endif
KeyMonitor(void *arg) // Direction Control:w,s,a,d-->Up Down Left Right
{
char k;
while (1) {
#if defined(_WIN16) || defined(_WIN32) || defined(_WIN64)
k = _getch();
#elif defined(__linux__) || defined(__gnu_linux__)
k = getchar();
#elif defined(__APPLE__)
#endif
switch (k) {
case 'w': // Up
{
directiontemp = 2;
break;
}
case 's': // Down
{
directiontemp = -2;
break;
}
case 'a': // Left
{
directiontemp = -1;
break;
}
case 'd': // Right
{
directiontemp = 1;
break;
}
case 'j': // SpeedUp
{
delay = delay * 4 / 5;
break;
}
case 'k': // SpeedDown
{
delay = delay * 5 / 4;
break;
}
case 27: // ESC
{
printf("\nExit!\n");
isPause = 0;
direction = 0;
directiontemp = 0;
#if defined(_WIN16) || defined(_WIN32) || defined(_WIN64)
return 0;
#elif defined(__linux__) || defined(__gnu_linux__)
return NULL;
#elif defined(__APPLE__)
#endif
break;
}
case ' ': // Space
{
if (isPause) {
printf("\nContinue!\n");
} else {
printf("\nPause!\n");
}
isPause = !isPause;
break;
}
}
}
}
#endif /* _KEYMONITOR_H */