Further OSM improvements:

locking support (double-tap to lock, tap to unlock or use KC_NO)
 includes word-lock (shift-lock: ' ' -> _)
This commit is contained in:
bluepython508
2024-03-05 22:05:50 +00:00
parent c65beba3bb
commit 373ccbec40

View File

@@ -13,23 +13,23 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "keycodes.h"
#include "action.h"
#include QMK_KEYBOARD_H
enum keycodes {
OS_LSFT = SAFE_RANGE,
OS_LCTL,
OS_LALT,
OS_LGUI,
OS_LSFT = SAFE_RANGE,
OS_LCTL,
OS_LALT,
OS_LGUI,
};
enum layers {
_COLEMAK_DH = 0,
_GAME,
_SYM,
_NUM,
_NAV,
_FUN,
_COLEMAK_DH = 0,
_GAME,
_SYM,
_NUM,
_NAV,
_FUN,
};
// Aliases for readability
@@ -204,6 +204,7 @@ typedef enum {
os_up_queued,
os_down_unused,
os_down_used,
os_locked
} oneshot_state_t;
oneshot_state_t oneshot_state[4] = {0};
@@ -227,70 +228,84 @@ bool is_oneshot_ignored_key(uint16_t keycode) {
bool is_oneshot_cancel_key(uint16_t keycode) {
switch (keycode) {
case NAV:
case SYM:
case NUM:
case FUN:
// case NAV:
// case SYM:
// case NUM:
// case FUN:
// return true;
case KC_NO:
return true;
default:
return false;
}
}
// From https://github.com/qmk/qmk_firmware/blob/user-keymaps-still-present/users/callum/oneshot.c
// Based on https://github.com/qmk/qmk_firmware/blob/user-keymaps-still-present/users/callum/oneshot.c
void update_oneshot(
oneshot_state_t *state,
uint16_t mod,
uint16_t trigger,
uint16_t keycode,
keyrecord_t *record
) {
if (keycode == trigger) {
if (record->event.pressed) {
// Trigger keydown
if (*state == os_up_unqueued) {
register_code(mod);
}
*state = os_down_unused;
} else {
// Trigger keyup
switch (*state) {
case os_down_unused:
// If we didn't use the mod while trigger was held, queue it.
*state = os_up_queued;
break;
case os_down_used:
// If we did use the mod while trigger was held, unregister it.
*state = os_up_unqueued;
unregister_code(mod);
break;
default:
break;
}
}
oneshot_state_t *state,
uint16_t mod,
uint16_t trigger,
uint16_t keycode,
keyrecord_t *record
) {
if (keycode == trigger) {
if (record->event.pressed) {
// Trigger keydown
switch (*state) {
case os_up_queued:
*state = os_locked;
break;
case os_up_unqueued:
register_code(mod);
*state = os_down_unused;
break;
case os_locked:
*state = os_down_used;
break;
default:
*state = os_down_unused;
break;
}
} else {
if (record->event.pressed) {
if (is_oneshot_cancel_key(keycode) && *state != os_up_unqueued) {
// Cancel oneshot on designated cancel keydown.
*state = os_up_unqueued;
unregister_code(mod);
}
} else {
if (!is_oneshot_ignored_key(keycode)) {
// On non-ignored keyup, consider the oneshot used.
switch (*state) {
case os_down_unused:
*state = os_down_used;
break;
case os_up_queued:
*state = os_up_unqueued;
unregister_code(mod);
break;
default:
break;
}
}
}
// Trigger keyup
switch (*state) {
case os_down_unused:
// If we didn't use the mod while trigger was held, queue it.
*state = os_up_queued;
break;
case os_down_used:
// If we did use the mod while trigger was held, unregister it.
*state = os_up_unqueued;
unregister_code(mod);
break;
default:
break;
}
}
} else {
if (record->event.pressed) {
if (is_oneshot_cancel_key(keycode) && *state != os_up_unqueued) {
// Cancel oneshot on designated cancel keydown.
*state = os_up_unqueued;
unregister_code(mod);
}
} else {
if (!is_oneshot_ignored_key(keycode)) {
// On non-ignored keyup, consider the oneshot used.
switch (*state) {
case os_down_unused:
*state = os_down_used;
break;
case os_up_queued:
*state = os_up_unqueued;
unregister_code(mod);
break;
default:
break;
}
}
}
}
}
bool process_record_user(uint16_t keycode, keyrecord_t *record) {
@@ -299,6 +314,15 @@ bool process_record_user(uint16_t keycode, keyrecord_t *record) {
update_oneshot(&oneshot_state[2], mods[2], OS_LALT, keycode, record);
update_oneshot(&oneshot_state[3], mods[3], OS_LGUI, keycode, record);
if (oneshot_state[0] == os_locked && keycode == KC_SPACE) {
if (record->event.pressed) {
register_code(KC_MINUS);
} else {
unregister_code(KC_MINUS);
}
return false;
}
return true;
}