]> git.donarmstrong.com Git - qmk_firmware.git/blobdiff - quantum/quantum.c
fix for issue #1568
[qmk_firmware.git] / quantum / quantum.c
index 1f8ce6c46f52464817f37f5777256c8d27a75428..65213eaea7b369f901c4a3679a35077ea892ba21 100644 (file)
@@ -40,13 +40,12 @@ extern backlight_config_t backlight_config;
   #ifndef AG_SWAP_SONG
     #define AG_SWAP_SONG SONG(AG_SWAP_SOUND)
   #endif
-  #ifndef DEFAULT_LAYER_SONGS
-    #define DEFAULT_LAYER_SONGS { }
-  #endif
   float goodbye_song[][2] = GOODBYE_SONG;
   float ag_norm_song[][2] = AG_NORM_SONG;
   float ag_swap_song[][2] = AG_SWAP_SONG;
-  float default_layer_songs[][16][2] = DEFAULT_LAYER_SONGS;
+  #ifdef DEFAULT_LAYER_SONGS
+    float default_layer_songs[][16][2] = DEFAULT_LAYER_SONGS;
+  #endif
 #endif
 
 static void do_code16 (uint16_t code, void (*f) (uint8_t)) {
@@ -162,6 +161,11 @@ void reset_keyboard(void) {
 static bool shift_interrupted[2] = {0, 0};
 static uint16_t scs_timer[2] = {0, 0};
 
+/* true if the last press of GRAVE_ESC was shifted (i.e. GUI or SHIFT were pressed), false otherwise.
+ * Used to ensure that the correct keycode is released if the key is released.
+ */
+static bool grave_esc_was_shifted = false;
+
 bool process_record_quantum(keyrecord_t *record) {
 
   /* This gets the keycode from the key pressed */
@@ -193,6 +197,10 @@ bool process_record_quantum(keyrecord_t *record) {
     // }
 
   if (!(
+  #if defined(KEY_LOCK_ENABLE)
+    // Must run first to be able to mask key_up events.
+    process_key_lock(&keycode, record) &&
+  #endif
     process_record_kb(keycode, record) &&
   #if defined(MIDI_ENABLE) && defined(MIDI_ADVANCED)
     process_midi(keycode, record) &&
@@ -472,12 +480,25 @@ bool process_record_quantum(keyrecord_t *record) {
       // break;
     }
     case GRAVE_ESC: {
-      void (*method)(uint8_t) = (record->event.pressed) ? &add_key : &del_key;
       uint8_t shifted = get_mods() & ((MOD_BIT(KC_LSHIFT)|MOD_BIT(KC_RSHIFT)
                                       |MOD_BIT(KC_LGUI)|MOD_BIT(KC_RGUI)));
 
-      method(shifted ? KC_GRAVE : KC_ESCAPE);
-      send_keyboard_report(); 
+#ifdef GRAVE_ESC_CTRL_OVERRIDE
+      // if CTRL is pressed, ESC is always read as ESC, even if SHIFT or GUI is pressed.
+      // this is handy for the ctrl+shift+esc shortcut on windows, among other things.
+      if (get_mods() & (MOD_BIT(KC_LCTL) | MOD_BIT(KC_RCTL)))
+        shifted = 0;
+#endif
+
+      if (record->event.pressed) {
+        grave_esc_was_shifted = shifted;
+        add_key(shifted ? KC_GRAVE : KC_ESCAPE);
+      }
+      else {
+        del_key(grave_esc_was_shifted ? KC_GRAVE : KC_ESCAPE);
+      }
+
+      send_keyboard_report();
     }
     default: {
       shift_interrupted[0] = true;
@@ -556,7 +577,7 @@ void send_string_with_delay(const char *str, uint8_t interval) {
 }
 
 void set_single_persistent_default_layer(uint8_t default_layer) {
-  #ifdef AUDIO_ENABLE
+  #if defined(AUDIO_ENABLE) && defined(DEFAULT_LAYER_SONGS)
     PLAY_SONG(default_layer_songs[default_layer]);
   #endif
   eeconfig_update_default_layer(1U<<default_layer);