diff --git a/src/Kaleidoscope.h b/src/Kaleidoscope.h
index 57dbbe81..bf457698 100644
--- a/src/Kaleidoscope.h
+++ b/src/Kaleidoscope.h
@@ -80,6 +80,7 @@ void setup();
#endif
#include "kaleidoscope/KeyAddr.h"
+#include "kaleidoscope/KeyEvent.h"
#include "kaleidoscope/key_events.h"
#include "kaleidoscope/layers.h"
#include "kaleidoscope_internal/sketch_exploration/sketch_exploration.h"
diff --git a/src/kaleidoscope/KeyEvent.cpp b/src/kaleidoscope/KeyEvent.cpp
new file mode 100644
index 00000000..c9d01704
--- /dev/null
+++ b/src/kaleidoscope/KeyEvent.cpp
@@ -0,0 +1,23 @@
+/* Kaleidoscope - Firmware for computer input devices
+ * Copyright (C) 2020 Keyboard.io, Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License as published by the Free Software
+ * Foundation, version 3.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program. If not, see .
+ */
+
+#include "kaleidoscope/KeyEvent.h"
+
+namespace kaleidoscope {
+
+KeyEventId KeyEvent::last_id_ = 0;
+
+} // namespace kaleidoscope
diff --git a/src/kaleidoscope/KeyEvent.h b/src/kaleidoscope/KeyEvent.h
new file mode 100644
index 00000000..0cc97ab8
--- /dev/null
+++ b/src/kaleidoscope/KeyEvent.h
@@ -0,0 +1,68 @@
+/* Kaleidoscope - Firmware for computer input devices
+ * Copyright (C) 2021 Keyboard.io, Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License as published by the Free Software
+ * Foundation, version 3.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program. If not, see .
+ */
+
+#pragma once
+
+#include // for uint8_t, int8_t
+
+#include "kaleidoscope/key_defs.h" // for Key, Key_NoKey
+#include "kaleidoscope/KeyAddr.h" // for KeyAddr
+
+namespace kaleidoscope {
+
+// It's important that this is a signed integer, not unsigned. It should be
+// `int8_t`, but I changed it to 32 for debugging purposes.
+typedef int8_t KeyEventId;
+
+struct KeyEvent {
+
+ public:
+ // Constructor for plugin use when regenerating an event with specific ID:
+ KeyEvent(KeyAddr addr, uint8_t state,
+ Key key = Key_NoKey, KeyEventId id = last_id_)
+ : addr(addr), state(state), key(key), id_(id) {}
+
+ KeyEvent() : id_(last_id_) {}
+
+ // For use by keyscanner creating a new event from a physical keyswitch toggle
+ // on or off.
+ static KeyEvent next(KeyAddr addr, uint8_t state) {
+ return KeyEvent(addr, state, Key_NoKey, ++last_id_);
+ }
+
+ KeyEventId id() const {
+ return id_;
+ }
+ void swapId(KeyEvent &other) {
+ KeyEventId tmp_id = id_;
+ id_ = other.id_;
+ other.id_ = tmp_id;
+ }
+
+ KeyAddr addr = KeyAddr::none();
+ uint8_t state = 0;
+ Key key = Key_NoKey;
+
+ private:
+ // serial number of the event:
+ static KeyEventId last_id_;
+ KeyEventId id_;
+};
+
+} // namespace kaleidoscope
+
+typedef kaleidoscope::KeyEventId KeyEventId;
+typedef kaleidoscope::KeyEvent KeyEvent;