-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeys.cpp
109 lines (91 loc) · 2.65 KB
/
keys.cpp
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
105
106
107
108
109
//------------------------------------------------------------------------------
/// \file keys.cpp
/// \author Rob Bateman
/// \date 8-3-2005
/// \brief
///
//------------------------------------------------------------------------------
#include "keys.h"
#include <stdlib.h>
#include <stdio.h>
#include <GL/glut.h>
// callback functions
key_func g_upfunc=0;
key_func g_downfunc=0;
//--------------------------------------------------------------------------
/// \brief this sets the keyboard press callback
/// \param func - the function to handle key presses
///
void SetKeyboardFunc(key_func func) {
g_downfunc = func;
}
//--------------------------------------------------------------------------
/// \brief this sets the keyboard release callback
/// \param func - the function to handle key releases
///
void SetKeyboardUpFunc(key_func func) {
g_upfunc = func;
}
// the state of all keys. We need to store this so that we can find out if more
// than one key is pressed at any given time.
//
bool g_keystates[512]={false};
//-------------------------------------------------------------------------- IsKeyPressed
//
bool IsKeyPressed(int key) {
// make sure valid key requested
if (key>=0&&key<512) {
// return the state of the requested key
return g_keystates[key];
}
return false;
}
//-------------------------------------------------------------------------- KeyUpFunc
//
void KeyUpFunc(unsigned char key,int x,int y) {
// set state of key
g_keystates[key]=false;
// if callback function set, call it
if (g_upfunc) {
g_upfunc(key,false);
}
}
//-------------------------------------------------------------------------- KeyDownFunc
//
void KeyDownFunc(unsigned char key,int x,int y) {
// set state of key
g_keystates[key]=true;
// if callback function set, call it
if (g_downfunc) {
g_downfunc(key,true);
}
}
//-------------------------------------------------------------------------- SpecialKeyUpFunc
//
void SpecialKeyUpFunc(int key,int x,int y) {
// set state of key
g_keystates[key+256]=false;
// if callback function set, call it
if (g_upfunc) {
g_upfunc(key+256,false);
}
}
//-------------------------------------------------------------------------- SpecialKeyDownFunc
//
void SpecialKeyDownFunc(int key,int x,int y) {
// set state of key
g_keystates[key+256]=true;
// if callback function set, call it
if (g_downfunc) {
g_downfunc(key+256,true);
}
}
//-------------------------------------------------------------------------- InitKeyboard
//
void InitKeyboard() {
// set the glutcallback functions
glutKeyboardFunc(KeyDownFunc);
glutKeyboardUpFunc(KeyUpFunc);
glutSpecialFunc(SpecialKeyDownFunc);
glutSpecialUpFunc(SpecialKeyUpFunc);
}