@ -18,6 +18,8 @@
# include "kaleidoscope/hooks.h"
# include "kaleidoscope/layers.h"
# include "kaleidoscope/keyswitch_state.h"
# include "kaleidoscope/KeyEvent.h"
# include "kaleidoscope/LiveKeys.h"
// The maximum number of layers allowed. `layer_state_`, which stores
// the on/off status of the layers in a bitfield has only 32 bits, and
@ -54,30 +56,36 @@ void Layer_::setup() {
Layer . updateActiveLayers ( ) ;
}
void Layer_ : : handleKeymapKeyswitchEvent ( Key keymapEntry , uint8_t keyState ) {
if ( keymapEntry . getKeyCode ( ) > = LAYER_MOVE_OFFSET ) {
void Layer_ : : handleLayerKeyEvent ( const KeyEvent & event ) {
// The caller is responsible for checking that this is a Layer `Key`, so we
// skip checking for it here.
uint8_t key_code = event . key . getKeyCode ( ) ;
uint8_t target_layer ;
if ( key_code > = LAYER_MOVE_OFFSET ) {
// MoveToLayer()
if ( keyToggledOn ( keyState ) ) {
move ( keymapEntry . getKeyCode ( ) - LAYER_MOVE_OFFSET ) ;
if ( keyToggledOn ( event . state ) ) {
target_layer = key_code - LAYER_MOVE_OFFSET ;
move ( target_layer ) ;
}
} else if ( keymapEntry . getKeyCode ( ) > = LAYER_SHIFT_OFFSET ) {
// layer shift keys
uint8_t target = keymapEntry . getKeyCode ( ) - LAYER_SHIFT_OFFSET ;
} else if ( key _code > = LAYER_SHIFT_OFFSET ) {
// layer shift keys (two types)
target_layer = key_code - LAYER_SHIFT_OFFSET ;
switch ( target ) {
switch ( target _layer ) {
case KEYMAP_NEXT :
// Key_KeymapNext_Momentary
if ( keyToggledOn ( keyS tate) )
if ( keyToggledOn ( event. s tate) )
activateNext ( ) ;
else if ( keyToggledOff ( keyState ) )
else
deactivateMostRecent ( ) ;
break ;
case KEYMAP_PREVIOUS :
// Key_KeymapPrevious_Momentary
if ( keyToggledOn ( keyS tate) )
if ( keyToggledOn ( event. s tate) )
deactivateMostRecent ( ) ;
else if ( keyToggledOff ( keyState ) )
else
activateNext ( ) ;
break ;
@ -97,30 +105,48 @@ void Layer_::handleKeymapKeyswitchEvent(Key keymapEntry, uint8_t keyState) {
* that does turn the layer off , but with the other still being held , the
* layer will toggle back on in the same cycle .
*/
if ( keyIsPressed ( keyState ) ) {
if ( ! Layer . isActive ( target ) )
activate ( target ) ;
} else if ( keyToggledOff ( keyState ) ) {
deactivate ( target ) ;
if ( keyToggledOn ( event . state ) ) {
// Re-think this: maybe we want to bring an already-active layer to the
// top when a layer shift key is pressed.
if ( ! isActive ( target_layer ) )
activate ( target_layer ) ;
} else {
// If there's another layer shift key keeping the target layer active,
// we need to abort before deactivating it.
for ( Key key : live_keys . all ( ) ) {
if ( key = = event . key ) {
return ;
}
}
// No other layer shift key for the target layer is pressed; deactivate
// it now.
deactivate ( target_layer ) ;
}
break ;
}
} else if ( keyToggledOn ( keyState ) ) {
} else if ( keyToggledOn ( event. s tate) ) {
// LockLayer()/UnlockLayer()
uint8_t target = keymapEntry . getKeyCode ( ) ;
target_layer = key_code ;
// switch keymap and stay there
if ( Layer . isActive ( target ) )
deactivate ( keymapEntry . getKeyCode ( ) ) ;
if ( isActive( target _layer ) )
deactivate ( target_layer ) ;
else
activate ( keymapEntry . getKeyCode ( ) ) ;
activate ( target_layer ) ;
}
}
# ifndef NDEPRECATED
void Layer_ : : handleKeymapKeyswitchEvent ( Key key , uint8_t key_state ) {
if ( key . getFlags ( ) = = ( SYNTHETIC | SWITCH_TO_KEYMAP ) )
handleLayerKeyEvent ( KeyEvent ( KeyAddr : : none ( ) , key_state , key ) ) ;
}
Key Layer_ : : eventHandler ( Key mappedKey , KeyAddr key_addr , uint8_t keyState ) {
if ( mappedKey . getFlags ( ) = = ( SYNTHETIC | SWITCH_TO_KEYMAP ) )
handleKeymapKeyswitchEvent ( mappedKey , keyState ) ;
handle LayerKeyEvent( KeyEvent ( key_addr , keyState , mappedKey ) ) ;
return mappedKey ;
}
# endif
Key Layer_ : : getKeyFromPROGMEM ( uint8_t layer , KeyAddr key_addr ) {
return keyFromKeymap ( layer , key_addr ) ;