-
Notifications
You must be signed in to change notification settings - Fork 0
/
ComputerKeyboard.h
57 lines (46 loc) · 1.38 KB
/
ComputerKeyboard.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
51
52
53
54
55
56
57
#pragma once
#include "Keyboard.h"
#include <iostream>
#include <map>
#include <string>
// For Windows
#ifdef _WIN32
#include <conio.h>
#else
// For Unix/Linux
#include <ncurses.h>
#endif
class ComputerKeyboard final : public Keyboard {
using UserInput = char;
public:
Input getInput() const final {
char ch;
#ifdef _WIN32
// Windows implementation using _kbhit and _getch from conio.h
while (!_kbhit())
; // Wait for a key press
ch = _getch();
#else
// Unix/Linux implementation using ncurses
initscr(); // Start curses mode
cbreak(); // Line buffering disabled
noecho(); // Don't echo while we do getch
ch = getch();
endwin(); // End curses mode
#endif
std::clog << "Key Pressed: " << ch << "\n";
auto it = keys_.find(ch);
return it != keys_.end() ? it->second : InvalidKey::Unkown;
}
private:
static std::map<UserInput, Input> keys_;
};
std::map<ComputerKeyboard::UserInput, Input> ComputerKeyboard::keys_ = {
{'a', PianoKey::C_lower}, {'w', PianoKey::Cs},
{'s', PianoKey::D}, {'e', PianoKey::Ds},
{'d', PianoKey::E}, {'f', PianoKey::F},
{'t', PianoKey::Fs}, {'g', PianoKey::G},
{'y', PianoKey::Gs}, {'h', PianoKey::A},
{'u', PianoKey::As}, {'j', PianoKey::B},
{'k', PianoKey::C_upper}, {',', ControlKey::OctaveDown},
{'.', ControlKey::OctaveUp}};