forked from githole/Live-Coder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKeyAnalyzer.cpp
136 lines (122 loc) · 2.99 KB
/
KeyAnalyzer.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include "KeyAnalyzer.h"
namespace LiveCoder {
bool IsMoveKey(SDLKey key) {
if (key == SDLK_UP || key == SDLK_DOWN || key == SDLK_LEFT || key == SDLK_RIGHT ||
key == SDLK_HOME || key == SDLK_END ||
key == 'p' || key == 'P' || key == 'n' || key == 'N' || key == 'b' || key == 'B' || key == 'f' || key == 'F') {
return true;
}
return false;
}
void KeyAnalyzer::KeyHit(TextEditor* textEditor, SDLKey key, SDLMod mod, const std::string& filename) {
/*
if ((mod & KMOD_SHIFT) && IsMoveKey(key)) {
if (!textEditor->IsSelectMode())
textEditor->BeginSelect();
} else {
textEditor->EndSelect();
}*/
if (IsMoveKey(key)) {
if (mod & KMOD_SHIFT) {
if (!textEditor->IsSelectMode())
textEditor->BeginSelect();
} else {
textEditor->EndSelect();
}
}
if (mod & KMOD_CTRL) {
// unix like keybind
if (key == 'p' || key == 'P')
textEditor->MoveCursor(ECMT_UP);
else if (key == 'n' || key == 'N')
textEditor->MoveCursor(ECMT_DOWN);
else if (key == 'b' || key == 'B')
textEditor->MoveCursor(ECMT_LEFT);
else if (key == 'f' || key == 'F')
textEditor->MoveCursor(ECMT_RIGHT);
// shortcut key
else if (key == 'z' || key == 'Z')
textEditor->Undo();
else if (key == 'y' || key == 'Y')
textEditor->Redo();
else if (key == 's' || key == 'S')
textEditor->Save(filename);
else if (key == 'o' || key == 'O')
textEditor->Load(filename);
else if (key == 'a' || key == 'A')
textEditor->Home();
else if (key == 'e' || key == 'E')
textEditor->End();
else if (key == 'c' || key == 'C')
textEditor->Copy();
else if (key == 'x' || key == 'X') {
textEditor->Copy();
textEditor->DeleteSelectedArea();
} else if (key == 'v' || key == 'V')
textEditor->Paste();
else if (key == SDLK_HOME)
textEditor->MoveHead();
else if (key == SDLK_END)
textEditor->MoveTail();
} else if (32 <= key && key <= 126) {
if (textEditor->IsSelectMode()) {
textEditor->DeleteSelectedArea();
}
textEditor->InsertCharacter(key);
} else {
switch (key) {
case SDLK_UP:
textEditor->MoveCursor(ECMT_UP);
break;
case SDLK_DOWN:
textEditor->MoveCursor(ECMT_DOWN);
break;
case SDLK_LEFT:
textEditor->MoveCursor(ECMT_LEFT);
break;
case SDLK_RIGHT:
textEditor->MoveCursor(ECMT_RIGHT);
break;
case SDLK_PAGEUP:
textEditor->PageUp();
break;
case SDLK_PAGEDOWN:
textEditor->PageDown();
break;
case SDLK_HOME:
textEditor->Home();
break;
case SDLK_END:
textEditor->End();
break;
case SDLK_RETURN:
textEditor->InsertCharacter('\n');
break;
case SDLK_BACKSPACE:
if (textEditor->IsSelectMode()) {
textEditor->DeleteSelectedArea();
} else {
textEditor->Backspace();
}
break;
case SDLK_DELETE:
if (textEditor->IsSelectMode()) {
textEditor->DeleteSelectedArea();
} else {
textEditor->Delete();
}
break;
case SDLK_TAB:
textEditor->InsertCharacter(' ');
textEditor->InsertCharacter(' ');
break;
}
}
}
KeyAnalyzer::KeyAnalyzer(void)
{
}
KeyAnalyzer::~KeyAnalyzer(void)
{
}
}