Skip to content

Commit 166c1b5

Browse files
committed
Refactor keyboard event classes into keyboard_event.h
1 parent 997e911 commit 166c1b5

File tree

4 files changed

+142
-55
lines changed

4 files changed

+142
-55
lines changed

src/capi/keyboard_monitor_c.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "keyboard_monitor_c.h"
22
#include "../keyboard_monitor.h"
3-
#include "../keyboard_events.h"
3+
#include "../keyboard_event.h"
44
#include <memory>
55

66
// Internal structure to hold C API state

src/keyboard_event.h

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
#pragma once
2+
#include <string>
3+
#include <typeindex>
4+
#include "event.h"
5+
6+
namespace nativeapi {
7+
8+
/**
9+
* Base class for all keyboard-related events
10+
*
11+
* This class provides common functionality for keyboard events,
12+
* including access to the keycode that triggered the event.
13+
*/
14+
class KeyboardEvent : public Event {
15+
public:
16+
/**
17+
* Constructor for KeyboardEvent
18+
* @param keycode The keycode associated with this event
19+
*/
20+
explicit KeyboardEvent(int keycode) : keycode_(keycode) {}
21+
22+
/**
23+
* Virtual destructor
24+
*/
25+
virtual ~KeyboardEvent() = default;
26+
27+
/**
28+
* Get the keycode associated with this event
29+
* @return The keycode value
30+
*/
31+
int GetKeycode() const { return keycode_; }
32+
33+
/**
34+
* Get a string representation of the event type (for debugging)
35+
* Default implementation returns "KeyboardEvent"
36+
*/
37+
std::string GetTypeName() const override { return "KeyboardEvent"; }
38+
39+
private:
40+
int keycode_;
41+
};
42+
43+
/**
44+
* Event class for key press
45+
*
46+
* This event is emitted when a key is pressed down.
47+
*/
48+
class KeyPressedEvent : public KeyboardEvent {
49+
public:
50+
explicit KeyPressedEvent(int keycode) : KeyboardEvent(keycode) {}
51+
52+
/**
53+
* Get a string representation of the event type
54+
*/
55+
std::string GetTypeName() const override { return "KeyPressedEvent"; }
56+
57+
/**
58+
* Get the static type index for this event type
59+
*/
60+
static std::type_index GetStaticType() {
61+
return std::type_index(typeid(KeyPressedEvent));
62+
}
63+
64+
/**
65+
* Get the type index for this event instance
66+
*/
67+
std::type_index GetType() const {
68+
return GetStaticType();
69+
}
70+
};
71+
72+
/**
73+
* Event class for key release
74+
*
75+
* This event is emitted when a key is released.
76+
*/
77+
class KeyReleasedEvent : public KeyboardEvent {
78+
public:
79+
explicit KeyReleasedEvent(int keycode) : KeyboardEvent(keycode) {}
80+
81+
/**
82+
* Get a string representation of the event type
83+
*/
84+
std::string GetTypeName() const override { return "KeyReleasedEvent"; }
85+
86+
/**
87+
* Get the static type index for this event type
88+
*/
89+
static std::type_index GetStaticType() {
90+
return std::type_index(typeid(KeyReleasedEvent));
91+
}
92+
93+
/**
94+
* Get the type index for this event instance
95+
*/
96+
std::type_index GetType() const {
97+
return GetStaticType();
98+
}
99+
};
100+
101+
/**
102+
* Event class for modifier keys change
103+
*
104+
* This event is emitted when modifier keys (Ctrl, Alt, Shift, etc.) change state.
105+
*/
106+
class ModifierKeysChangedEvent : public KeyboardEvent {
107+
public:
108+
explicit ModifierKeysChangedEvent(uint32_t modifier_keys)
109+
: KeyboardEvent(0), modifier_keys_(modifier_keys) {}
110+
111+
/**
112+
* Get the modifier keys state
113+
* @return The modifier keys bitmask
114+
*/
115+
uint32_t GetModifierKeys() const { return modifier_keys_; }
116+
117+
/**
118+
* Get a string representation of the event type
119+
*/
120+
std::string GetTypeName() const override { return "ModifierKeysChangedEvent"; }
121+
122+
/**
123+
* Get the static type index for this event type
124+
*/
125+
static std::type_index GetStaticType() {
126+
return std::type_index(typeid(ModifierKeysChangedEvent));
127+
}
128+
129+
/**
130+
* Get the type index for this event instance
131+
*/
132+
std::type_index GetType() const {
133+
return GetStaticType();
134+
}
135+
136+
private:
137+
uint32_t modifier_keys_;
138+
};
139+
140+
} // namespace nativeapi

src/keyboard_events.h

Lines changed: 0 additions & 53 deletions
This file was deleted.

src/keyboard_monitor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
#include "event_dispatcher.h"
88
#include "event_emitter.h"
9-
#include "keyboard_events.h"
9+
#include "keyboard_event.h"
1010

1111
namespace nativeapi {
1212

0 commit comments

Comments
 (0)