-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKeyboardEventHandler.cpp
83 lines (78 loc) · 2.07 KB
/
KeyboardEventHandler.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
#include "KeyboardEventHandler.h"
bool KeyboardEventHandler::addFunction(int whatKey, FunctionType newFunction)
{
if ( keyFuncMap.end() != keyFuncMap.find( whatKey ))
{
std::cout << "duplicate key '" << whatKey << "' ignored." << std::endl;
return false;
}
else
{
keyFuncMap[whatKey].keyFunction = newFunction;
return true;
}
}
bool KeyboardEventHandler::addFunction (int whatKey, KeyStatusType keyPressStatus, FunctionType newFunction)
{
if (keyPressStatus == KEY_DOWN)
{
return addFunction(whatKey,newFunction);
}
else
{
if ( keyUPFuncMap.end() != keyUPFuncMap.find( whatKey ))
{
std::cout << "duplicate key '" << whatKey << "' ignored." << std::endl;
return false;
}
else
{
keyUPFuncMap[whatKey].keyFunction = newFunction;
return true;
}
} // KEY_UP
}
bool KeyboardEventHandler::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa)
{
bool newKeyDownEvent = false;
bool newKeyUpEvent = false;
switch(ea.getEventType())
{
case(osgGA::GUIEventAdapter::KEYDOWN):
{
KeyFunctionMap::iterator itr = keyFuncMap.find(ea.getKey());
if (itr != keyFuncMap.end())
{
if ( (*itr).second.keyState == KEY_UP )
{
(*itr).second.keyState = KEY_DOWN;
newKeyDownEvent = true;
}
if (newKeyDownEvent)
{
(*itr).second.keyFunction();
newKeyDownEvent = false;
}
return true;
}
return false;
}
case(osgGA::GUIEventAdapter::KEYUP):
{
KeyFunctionMap::iterator itr = keyFuncMap.find(ea.getKey());
if (itr != keyFuncMap.end() )
{
(*itr).second.keyState = KEY_UP;
}
itr = keyUPFuncMap.find(ea.getKey());
if (itr != keyUPFuncMap.end())
{
(*itr).second.keyFunction();
return true;
}
return false;
}
default:
return false;
}
}