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