2017-10-10 02:26:39 +00:00
|
|
|
//
|
|
|
|
// Keyboard.hpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 10/9/17.
|
2018-05-13 19:19:52 +00:00
|
|
|
// Copyright 2017 Thomas Harte. All rights reserved.
|
2017-10-10 02:26:39 +00:00
|
|
|
//
|
|
|
|
|
2019-07-03 01:14:33 +00:00
|
|
|
#ifndef Inputs_Keyboard_hpp
|
|
|
|
#define Inputs_Keyboard_hpp
|
2017-10-10 02:26:39 +00:00
|
|
|
|
|
|
|
#include <vector>
|
2018-10-25 01:59:30 +00:00
|
|
|
#include <set>
|
2017-10-10 02:26:39 +00:00
|
|
|
|
|
|
|
namespace Inputs {
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Provides an intermediate idealised model of a modern-era computer keyboard
|
|
|
|
(so, heavily indebted to the current Windows and Mac layouts), allowing a host
|
|
|
|
machine to toggle states, while an interested party either observes or polls.
|
|
|
|
*/
|
|
|
|
class Keyboard {
|
|
|
|
public:
|
|
|
|
enum class Key {
|
|
|
|
Escape, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, PrintScreen, ScrollLock, Pause,
|
2019-08-02 20:15:34 +00:00
|
|
|
BackTick, k1, k2, k3, k4, k5, k6, k7, k8, k9, k0, Hyphen, Equals, Backspace,
|
|
|
|
Tab, Q, W, E, R, T, Y, U, I, O, P, OpenSquareBracket, CloseSquareBracket, Backslash,
|
2017-10-10 02:26:39 +00:00
|
|
|
CapsLock, A, S, D, F, G, H, J, K, L, Semicolon, Quote, Hash, Enter,
|
|
|
|
LeftShift, Z, X, C, V, B, N, M, Comma, FullStop, ForwardSlash, RightShift,
|
2017-10-13 02:25:02 +00:00
|
|
|
LeftControl, LeftOption, LeftMeta, Space, RightMeta, RightOption, RightControl,
|
2017-10-10 02:26:39 +00:00
|
|
|
Left, Right, Up, Down,
|
|
|
|
Insert, Home, PageUp, Delete, End, PageDown,
|
2019-11-09 23:02:14 +00:00
|
|
|
NumLock, KeypadSlash, KeypadAsterisk, KeypadDelete,
|
|
|
|
Keypad7, Keypad8, Keypad9, KeypadPlus,
|
|
|
|
Keypad4, Keypad5, Keypad6, KeypadMinus,
|
|
|
|
Keypad1, Keypad2, Keypad3, KeypadEnter,
|
|
|
|
Keypad0, KeypadDecimalPoint, KeypadEquals,
|
2017-10-13 02:25:02 +00:00
|
|
|
Help
|
2017-10-10 02:26:39 +00:00
|
|
|
};
|
|
|
|
|
2018-10-25 01:59:30 +00:00
|
|
|
/// Constructs a Keyboard that declares itself to observe all keys.
|
2019-09-22 17:48:50 +00:00
|
|
|
Keyboard(const std::set<Key> &essential_modifiers = {});
|
2018-10-25 01:59:30 +00:00
|
|
|
|
|
|
|
/// Constructs a Keyboard that declares itself to observe only members of @c observed_keys.
|
2019-09-22 17:48:50 +00:00
|
|
|
Keyboard(const std::set<Key> &observed_keys, const std::set<Key> &essential_modifiers);
|
2018-10-25 01:59:30 +00:00
|
|
|
|
2017-10-10 02:26:39 +00:00
|
|
|
// Host interface.
|
2018-04-16 01:11:30 +00:00
|
|
|
virtual void set_key_pressed(Key key, char value, bool is_pressed);
|
2017-10-13 02:25:02 +00:00
|
|
|
virtual void reset_all_keys();
|
2017-10-10 02:26:39 +00:00
|
|
|
|
2018-10-25 01:59:30 +00:00
|
|
|
/// @returns a set of all Keys that this keyboard responds to.
|
|
|
|
virtual const std::set<Key> &observed_keys();
|
|
|
|
|
2019-09-22 17:48:50 +00:00
|
|
|
/// @returns the list of modifiers that this keyboard considers 'essential' (i.e. both mapped and highly used).
|
|
|
|
virtual const std::set<Inputs::Keyboard::Key> &get_essential_modifiers();
|
|
|
|
|
|
|
|
/*!
|
2018-10-25 01:59:30 +00:00
|
|
|
@returns @c true if this keyboard, on its original machine, looked
|
|
|
|
like a complete keyboard — i.e. if a user would expect this keyboard
|
|
|
|
to be the only thing a real keyboard maps to.
|
2019-09-22 17:48:50 +00:00
|
|
|
|
|
|
|
So this would be true of something like the Amstrad CPC, which has a full
|
|
|
|
keyboard, but it would be false of something like the Sega Master System
|
|
|
|
which has some buttons that you'd expect an emulator to map to its host
|
|
|
|
keyboard but which does not offer a full keyboard.
|
2018-10-25 01:59:30 +00:00
|
|
|
*/
|
|
|
|
virtual bool is_exclusive();
|
|
|
|
|
2017-10-10 02:26:39 +00:00
|
|
|
// Delegate interface.
|
|
|
|
struct Delegate {
|
|
|
|
virtual void keyboard_did_change_key(Keyboard *keyboard, Key key, bool is_pressed) = 0;
|
2017-10-15 02:07:11 +00:00
|
|
|
virtual void reset_all_keys(Keyboard *keyboard) = 0;
|
2017-10-10 02:26:39 +00:00
|
|
|
};
|
|
|
|
void set_delegate(Delegate *delegate);
|
|
|
|
bool get_key_state(Key key);
|
2017-10-15 02:07:11 +00:00
|
|
|
|
2017-10-10 02:26:39 +00:00
|
|
|
private:
|
2018-10-25 01:59:30 +00:00
|
|
|
std::set<Key> observed_keys_;
|
2019-09-22 17:48:50 +00:00
|
|
|
std::set<Key> essential_modifiers_;
|
2017-10-10 02:26:39 +00:00
|
|
|
std::vector<bool> key_states_;
|
|
|
|
Delegate *delegate_ = nullptr;
|
2018-10-25 01:59:30 +00:00
|
|
|
bool is_exclusive_ = true;
|
2017-10-10 02:26:39 +00:00
|
|
|
};
|
2017-11-08 03:51:06 +00:00
|
|
|
|
2017-10-10 02:26:39 +00:00
|
|
|
}
|
|
|
|
|
2019-07-03 01:14:33 +00:00
|
|
|
#endif /* Inputs_Keyboard_hpp */
|