@@ 12,6 12,7 @@
SPDX-License-Identifier: MIT
*/
+#include <stdint.h>
#include <stdlib.h>
#include <avr/io.h>
#include "backlight.h"
@@ 74,6 75,7 @@ USB_ClassInfo_HID_Device_t MediaControl_HID_Interface =
uint8_t matrix_debounce[KBD_COLS*KBD_ROWS];
uint8_t matrix_state[KBD_COLS*KBD_ROWS];
+uint16_t matrix_key_when_pressed[KBD_COLS*KBD_ROWS];
int active_meta_mode = 0;
uint16_t last_meta_key = 0;
@@ 142,15 144,8 @@ int process_keyboard(uint8_t* resulting_scancodes) {
// check input COLs
for (int x=0; x<14; x++) {
- uint16_t keycode;
uint16_t loc = y*KBD_COLS+x;
- keycode = active_matrix[loc];
- // Fall through to the default layer if there isn't anything on this one.
- if (keycode == 0x00) {
- keycode = matrix[loc];
- }
-
uint8_t pressed = 0;
uint8_t debounced_pressed = 0;
@@ 183,6 178,23 @@ int process_keyboard(uint8_t* resulting_scancodes) {
}
debounced_pressed = matrix_state[loc];
+ // Only get the keycode from the current matrix if it's released.
+ // Otherwise, we use the keycode that this key had when it was last
+ // pressed. This ensures that the keycode doesn't change mid-press if,
+ // e.g. a layer key is released before a key that is affected by the
+ // layer.
+ uint16_t keycode;
+ if (debounced_pressed) {
+ keycode = matrix_key_when_pressed[loc];
+ } else {
+ keycode = active_matrix[loc];
+ // Fall through to the default layer if there isn't anything on this one.
+ if (keycode == 0x00) {
+ keycode = matrix[loc];
+ }
+ matrix_key_when_pressed[loc] = keycode;
+ }
+
if (debounced_pressed) {
total_pressed++;