cangine
Loading...
Searching...
No Matches
input.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <functional>
4#include <map>
5#include <string>
6#include <vector>
7
8#include "key.hpp"
9
10// Input class for cangine.
11// currently only supports keyboard input.
12//
13
14namespace cangine {
15 class Window;
16
23 class Input {
24 private:
25 static inline std::map<size_t, std::function<void(std::string)>> charEventHandlers;
26 static inline std::map<size_t, std::function<void(KeyEvent)>> keyEventHandlers;
27 static inline size_t nextID = 0;
32 static void handleCharEvent(std::string s);
38 static void handleKeyEvent(Key key, KeyEventType eventType);
39 friend class Window;
40
41 public:
47 static bool getKey(Key key);
53 static bool getKeyDown(Key key);
59 static bool getKeyUp(Key key);
60
66 static size_t registerKeyEventHandler(std::function<void(KeyEvent)> f);
71 static void unregisterKeyEventHandler(size_t id);
72
78 static size_t registerCharEventHandler(std::function<void(std::string)> f);
83 static void unregisterCharEventHandler(size_t id);
84 };
85}
Static facade for keyboard input queries and event handlers.
Definition input.hpp:23
static bool getKey(Key key)
Tests whether a key is currently held down.
Definition input.cpp:26
static std::map< size_t, std::function< void(std::string)> > charEventHandlers
Registered character-event callbacks by id.
Definition input.hpp:25
static void handleCharEvent(std::string s)
Dispatches a character event to all registered handlers.
Definition input.cpp:7
static void unregisterCharEventHandler(size_t id)
Unregisters a previously registered character-event handler.
Definition input.cpp:58
static bool getKeyUp(Key key)
Tests whether a key was released this frame.
Definition input.cpp:34
static bool getKeyDown(Key key)
Tests whether a key was pressed this frame.
Definition input.cpp:30
static std::map< size_t, std::function< void(KeyEvent)> > keyEventHandlers
Registered key-event callbacks by id.
Definition input.hpp:26
static size_t nextID
Next id to hand out when registering a handler.
Definition input.hpp:27
static void handleKeyEvent(Key key, KeyEventType eventType)
Dispatches a key event to all registered handlers.
Definition input.cpp:15
static size_t registerKeyEventHandler(std::function< void(KeyEvent)> f)
Registers a callback invoked on every key event.
Definition input.cpp:38
static void unregisterKeyEventHandler(size_t id)
Unregisters a previously registered key-event handler.
Definition input.cpp:46
static size_t registerCharEventHandler(std::function< void(std::string)> f)
Registers a callback invoked on every character event.
Definition input.cpp:50
Owns the application window, OpenGL context and the frame loop.
Definition window.hpp:34
Definition audiomanager.cpp:4
KeyEventType
The kind of keyboard event that occurred.
Definition key.hpp:43
Key
Platform-independent keyboard key identifiers.
Definition key.hpp:11
A keyboard event: which key and what happened to it.
Definition key.hpp:51