Custom oneshot functionality from qmk_firmware/users/callum.
This commit is contained in:
		
							parent
							
								
									b1bdfb2ce4
								
							
						
					
					
						commit
						3b3e683da8
					
				
							
								
								
									
										57
									
								
								oneshot.c
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										57
									
								
								oneshot.c
									
									
									
									
									
										Executable file
									
								
							@ -0,0 +1,57 @@
 | 
			
		||||
#include "oneshot.h"
 | 
			
		||||
 | 
			
		||||
void update_oneshot(
 | 
			
		||||
    oneshot_state *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;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    } 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;
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										32
									
								
								oneshot.h
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										32
									
								
								oneshot.h
									
									
									
									
									
										Executable file
									
								
							@ -0,0 +1,32 @@
 | 
			
		||||
// Taken from qmk_firmware/users/callum
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include QMK_KEYBOARD_H
 | 
			
		||||
 | 
			
		||||
// Represents the four states a oneshot key can be in
 | 
			
		||||
typedef enum {
 | 
			
		||||
    os_up_unqueued,
 | 
			
		||||
    os_up_queued,
 | 
			
		||||
    os_down_unused,
 | 
			
		||||
    os_down_used,
 | 
			
		||||
} oneshot_state;
 | 
			
		||||
 | 
			
		||||
// Custom oneshot mod implementation that doesn't rely on timers. If a mod is
 | 
			
		||||
// used while it is held it will be unregistered on keyup as normal, otherwise
 | 
			
		||||
// it will be queued and only released after the next non-mod keyup.
 | 
			
		||||
void update_oneshot(
 | 
			
		||||
    oneshot_state *state,
 | 
			
		||||
    uint16_t mod,
 | 
			
		||||
    uint16_t trigger,
 | 
			
		||||
    uint16_t keycode,
 | 
			
		||||
    keyrecord_t *record
 | 
			
		||||
);
 | 
			
		||||
 | 
			
		||||
// To be implemented by the consumer. Defines keys to cancel oneshot mods.
 | 
			
		||||
bool is_oneshot_cancel_key(uint16_t keycode);
 | 
			
		||||
 | 
			
		||||
// To be implemented by the consumer. Defines keys to ignore when determining
 | 
			
		||||
// whether a oneshot mod has been used. Setting this to modifiers and layer
 | 
			
		||||
// change keys allows stacking multiple oneshot modifiers, and carrying them
 | 
			
		||||
// between layers.
 | 
			
		||||
bool is_oneshot_ignored_key(uint16_t keycode);
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user