Features | Features | ||||
-------- | -------- | ||||
* Multi-layer keymap - Multiple keyboard layouts with layer switching. | |||||
* Multi-layer Keymap - Multiple keyboard layouts with layer switching. | |||||
* Mouse key - Mouse control with keyboard | * Mouse key - Mouse control with keyboard | ||||
* System Control Key - Power Down, Sleep, Wake Up and USB Remote Wake up | * System Control Key - Power Down, Sleep, Wake Up and USB Remote Wake up | ||||
* Media Control Key - Volume Down/Up, Mute, Next/Prev track, Play, Stop and etc | * Media Control Key - Volume Down/Up, Mute, Next/Prev track, Play, Stop and etc | ||||
***In `KEYMAP` definition you need to omit prefix part `KC_` of keycode to keep keymap compact.*** For example, just use `A` instead you place `KC_A` in `KEYMAP`. Some keycodes has 4-letter short name in addition to descriptive name, you'll prefer short one in `KEYMAP`. | ***In `KEYMAP` definition you need to omit prefix part `KC_` of keycode to keep keymap compact.*** For example, just use `A` instead you place `KC_A` in `KEYMAP`. Some keycodes has 4-letter short name in addition to descriptive name, you'll prefer short one in `KEYMAP`. | ||||
#### 1.1 Normal key | |||||
#### 1.0 Other key | |||||
- `KC_NO` for no aciton | - `KC_NO` for no aciton | ||||
- `KC_TRNS` for transparent layer | |||||
#### 1.1 Normal key | |||||
- `KC_A` to `KC_Z`, `KC_1` to `KC_0` for alpha numeric key | - `KC_A` to `KC_Z`, `KC_1` to `KC_0` for alpha numeric key | ||||
- `KC_MINS`, `KC_EQL`, `KC_GRV`, `KC_RBRC`, `KC_LBRC`, `KC_COMM`, `KC_DOT`, `KC_BSLS`, `KC_SLSH`, `KC_SCLN`, `KC_QUOT` | - `KC_MINS`, `KC_EQL`, `KC_GRV`, `KC_RBRC`, `KC_LBRC`, `KC_COMM`, `KC_DOT`, `KC_BSLS`, `KC_SLSH`, `KC_SCLN`, `KC_QUOT` | ||||
- `KC_ESC`, `KC_TAB`, `KC_SPC`, `KC_BSPC`, `KC_ENT`, `KC_DEL`, `KC_INS` | - `KC_ESC`, `KC_TAB`, `KC_SPC`, `KC_BSPC`, `KC_ENT`, `KC_DEL`, `KC_INS` | ||||
- `KC_LGUI` and `KC_RGUI` for Windows key or Command key in Mac | - `KC_LGUI` and `KC_RGUI` for Windows key or Command key in Mac | ||||
#### 1.3 Fn key | #### 1.3 Fn key | ||||
**`KC_FNnn`** are `Fn` keys which not given any action at the beginning unlike most of keycodes has its own action. To use these keys in `KEYMAP` you need to assign action you want at first. Action of `Fn` is defined in `fn_actions[]` and index of the array is identical with number part of `KC_FNnn`. Thus `KC_FN0` designates action defined in first element of the array. ***32 `Fn` keys can be defined at most.*** | |||||
`KC_FNnn` are `Fn` keys which not given any action at the beginning unlike most of keycodes has its own action. To use these keys in `KEYMAP` you need to assign action you want at first. Action of `Fn` is defined in `fn_actions[]` and index of the array is identical with number part of `KC_FNnn`. Thus `KC_FN0` designates action defined in first element of the array. ***32 `Fn` keys can be defined at most.*** | |||||
#### 1.4 Mousekey | #### 1.4 Mousekey | ||||
- `KC_MS_U`, `KC_MS_D`, `KC_MS_L`, `KC_MS_R` for mouse cursor | - `KC_MS_U`, `KC_MS_D`, `KC_MS_L`, `KC_MS_R` for mouse cursor | ||||
ACTION_LAYER_SET_TAP_KEY(layer, key) | ACTION_LAYER_SET_TAP_KEY(layer, key) | ||||
ACTION_LAYER_SET_TAP_TOGGLE(layer) | ACTION_LAYER_SET_TAP_TOGGLE(layer) | ||||
`Layer Bit` action XOR bits with `current layer`. `Layer Bit` action can take 0 to 8 as argument. | |||||
`Layer Bit` action XOR given bits with `current layer`. `Layer Bit` action can take 0 to 15 as argument. | |||||
ACTION_LAYER_BIT(bits) | ACTION_LAYER_BIT(bits) | ||||
ACTION_LAYER_BIT_TOGGLE(bits) | ACTION_LAYER_BIT_TOGGLE(bits) | ||||
License | License | ||||
------- | ------- | ||||
Under `GPL` 2 or later. Some protocol files are under `Modified BSD License`. | Under `GPL` 2 or later. Some protocol files are under `Modified BSD License`. | ||||
LUFA and PJRC stack have their own license respectively. | |||||
LUFA, PJRC and V-USB stack have their own license respectively. |
} | } | ||||
} | } | ||||
static action_t get_action(key_t key) | |||||
{ | |||||
action_t action = keymap_get_action(current_layer, key.pos.row, key.pos.col); | |||||
/* Transparently use default layer */ | |||||
if (action.code == ACTION_TRANSPARENT) { | |||||
// TODO: layer stacking | |||||
action = keymap_get_action(default_layer, key.pos.row, key.pos.col); | |||||
debug("TRNASPARENT: "); debug_hex16(action.code); debug("\n"); | |||||
} | |||||
return action; | |||||
} | |||||
static void process_action(keyrecord_t *record) | static void process_action(keyrecord_t *record) | ||||
{ | { | ||||
keyevent_t event = record->event; | keyevent_t event = record->event; | ||||
if (IS_NOEVENT(event)) { return; } | if (IS_NOEVENT(event)) { return; } | ||||
action_t action = keymap_get_action(current_layer, event.key.pos.row, event.key.pos.col); | |||||
//debug("action: "); debug_hex16(action.code); if (event.pressed) debug("d\n"); else debug("u\n"); | |||||
action_t action = get_action(event.key); | |||||
debug("ACTION: "); debug_action(action); debug("\n"); | debug("ACTION: "); debug_action(action); debug("\n"); | ||||
switch (action.kind.id) { | switch (action.kind.id) { | ||||
layer_switch(action.layer.val); | layer_switch(action.layer.val); | ||||
} | } | ||||
else { | else { | ||||
// NOTE: This is needed by legacy keymap support | |||||
layer_switch(default_layer); | layer_switch(default_layer); | ||||
} | } | ||||
break; | break; | ||||
unregister_code(action.layer.code); | unregister_code(action.layer.code); | ||||
} else { | } else { | ||||
//debug("LAYER_PRESSED: No tap: NO ACTION\n"); | //debug("LAYER_PRESSED: No tap: NO ACTION\n"); | ||||
//TODO: this is ok? | |||||
// NOTE: This is needed by legacy keymap support | |||||
debug("LAYER_PRESSED: No tap: return to default layer\n"); | debug("LAYER_PRESSED: No tap: return to default layer\n"); | ||||
layer_switch(default_layer); | layer_switch(default_layer); | ||||
} | } | ||||
bool is_tap_key(key_t key) | bool is_tap_key(key_t key) | ||||
{ | { | ||||
action_t action = keymap_get_action(current_layer, key.pos.row, key.pos.col); | |||||
action_t action = get_action(key); | |||||
switch (action.kind.id) { | switch (action.kind.id) { | ||||
case ACT_LMODS_TAP: | case ACT_LMODS_TAP: | ||||
case ACT_RMODS_TAP: | case ACT_RMODS_TAP: |
------------- | ------------- | ||||
ACT_LMODS(0000): | ACT_LMODS(0000): | ||||
0000|0000|000000|00 No action | 0000|0000|000000|00 No action | ||||
0000|0000|000000|01 Transparent | |||||
0000|0000| keycode Key | 0000|0000| keycode Key | ||||
0000|mods|000000|00 Left mods | 0000|mods|000000|00 Left mods | ||||
0000|mods| keycode Key & Left mods | 0000|mods| keycode Key & Left mods | ||||
ACT_RMODS(0001): | ACT_RMODS(0001): | ||||
0001|0000|000000|00 No action | |||||
0001|0000|000000|00 No action(not used) | |||||
0001|0000|000000|01 Transparent(not used) | |||||
0001|0000| keycode Key(no used) | 0001|0000| keycode Key(no used) | ||||
0001|mods|000000|00 Right mods | 0001|mods|000000|00 Right mods | ||||
0001|mods| keycode Key & Right mods | 0001|mods| keycode Key & Right mods | ||||
1000|LLLL|0000 0001 set L to layer on press | 1000|LLLL|0000 0001 set L to layer on press | ||||
1000|LLLL|0000 0010 set L to layer on release | 1000|LLLL|0000 0010 set L to layer on release | ||||
1000|----|0000 0011 set default to layer on both(return to default layer) | 1000|----|0000 0011 set default to layer on both(return to default layer) | ||||
1000|LLLL|xxxx xxxx set L to layer while hold and send key on tap | |||||
1000|LLLL| keycode set L to layer while hold and send key on tap | |||||
1000|LLLL|1111 0000 set L to layer while hold and toggle on several taps | 1000|LLLL|1111 0000 set L to layer while hold and toggle on several taps | ||||
1000|LLLL|1111 1111 set L to default and layer(on press) | 1000|LLLL|1111 1111 set L to default and layer(on press) | ||||
1001|BBBB|0000 0001 bit-xor layer with B on press | 1001|BBBB|0000 0001 bit-xor layer with B on press | ||||
1001|BBBB|0000 0010 bit-xor layer with B on release | 1001|BBBB|0000 0010 bit-xor layer with B on release | ||||
1001|BBBB|0000 0011 bit-xor layer with B on both(momentary) | 1001|BBBB|0000 0011 bit-xor layer with B on both(momentary) | ||||
1001|BBBB|xxxx xxxx bit-xor layer with B while hold and send key on tap | |||||
1001|BBBB| keycode bit-xor layer with B while hold and send key on tap | |||||
1001|BBBB|1111 0000 bit-xor layer with B while hold and toggle on several taps | 1001|BBBB|1111 0000 bit-xor layer with B while hold and toggle on several taps | ||||
1001|BBBB|1111 1111 bit-xor default with B and set layer(on press) | 1001|BBBB|1111 1111 bit-xor default with B and set layer(on press) | ||||
/* action utility */ | /* action utility */ | ||||
#define ACTION_NO 0 | #define ACTION_NO 0 | ||||
#define ACTION_TRANSPARENT 1 | |||||
#define ACTION(kind, param) ((kind)<<12 | (param)) | #define ACTION(kind, param) ((kind)<<12 | (param)) | ||||
#define MODS4(mods) (((mods)>>4 | (mods)) & 0x0F) | #define MODS4(mods) (((mods)>>4 | (mods)) & 0x0F) | ||||
/* Key */ | /* Key */ | ||||
#define ACTION_KEY(key) ACTION(ACT_LMODS, key) | #define ACTION_KEY(key) ACTION(ACT_LMODS, key) | ||||
/* Mods & key */ | /* Mods & key */ | ||||
#define ACTION_LMODS(mods) ACTION(ACT_LMODS, (mods)<<8 | 0x00) | |||||
#define ACTION_LMODS_KEY(mods, key) ACTION(ACT_LMODS, (mods)<<8 | (key)) | |||||
#define ACTION_RMODS(mods) ACTION(ACT_RMODS, (mods)<<8 | 0x00) | |||||
#define ACTION_RMODS_KEY(mods, key) ACTION(ACT_RMODS, (mods)<<8 | (key)) | |||||
#define ACTION_LMODS(mods) ACTION(ACT_LMODS, MODS4(mods)<<8 | 0x00) | |||||
#define ACTION_LMODS_KEY(mods, key) ACTION(ACT_LMODS, MODS4(mods)<<8 | (key)) | |||||
#define ACTION_RMODS(mods) ACTION(ACT_RMODS, MODS4(mods)<<8 | 0x00) | |||||
#define ACTION_RMODS_KEY(mods, key) ACTION(ACT_RMODS, MODS4(mods)<<8 | (key)) | |||||
/* Mod & key */ | /* Mod & key */ | ||||
#define ACTION_LMOD(mod) ACTION(ACT_LMODS, MODS4(MOD_BIT(mod))<<8 | 0x00) | #define ACTION_LMOD(mod) ACTION(ACT_LMODS, MODS4(MOD_BIT(mod))<<8 | 0x00) | ||||
#define ACTION_LMOD_KEY(mod, key) ACTION(ACT_LMODS, MODS4(MOD_BIT(mod))<<8 | (key)) | #define ACTION_LMOD_KEY(mod, key) ACTION(ACT_LMODS, MODS4(MOD_BIT(mod))<<8 | (key)) | ||||
/* | /* | ||||
* Set layer | * Set layer | ||||
*/ | */ | ||||
/* set layer on press and set default on release */ | |||||
#define ACTION_LAYER_SET(layer) ACTION_LAYER_SET_MOMENTARY(layer) | |||||
/* set layer on press and none on release */ | |||||
#define ACTION_LAYER_SET(layer) ACTION_LAYER_SET_P(layer) | |||||
/* set layer on press and set default on release (This is needed by legacy keymap support.) */ | |||||
#define ACTION_LAYER_SET_MOMENTARY(layer) ACTION(ACT_LAYER, (layer)<<8 | LAYER_MOMENTARY) | #define ACTION_LAYER_SET_MOMENTARY(layer) ACTION(ACT_LAYER, (layer)<<8 | LAYER_MOMENTARY) | ||||
/* set layer on press and none on release */ | /* set layer on press and none on release */ | ||||
#define ACTION_LAYER_SET_TOGGLE(layer) ACTION_LAYER_SET_R(layer) | #define ACTION_LAYER_SET_TOGGLE(layer) ACTION_LAYER_SET_R(layer) |
#endif | #endif | ||||
case KC_0: | case KC_0: | ||||
case KC_F10: | case KC_F10: | ||||
clear_keyboard(); | |||||
switch_layer(0); | switch_layer(0); | ||||
break; | break; | ||||
case KC_1: | |||||
case KC_F1: | |||||
switch_layer(1); | |||||
break; | |||||
case KC_2: | |||||
case KC_F2: | |||||
switch_layer(2); | |||||
break; | |||||
case KC_3: | |||||
case KC_F3: | |||||
switch_layer(3); | |||||
case KC_1 ... KC_9: | |||||
clear_keyboard(); | |||||
switch_layer((code - KC_1) + 1); | |||||
break; | break; | ||||
case KC_4: | |||||
case KC_F4: | |||||
switch_layer(4); | |||||
case KC_F1 ... KC_F9: | |||||
clear_keyboard(); | |||||
switch_layer((code - KC_F1) + 1); | |||||
break; | break; | ||||
default: | default: | ||||
print("?"); | print("?"); |
#define IS_KEY(code) (KC_A <= (code) && (code) <= KC_EXSEL) | #define IS_KEY(code) (KC_A <= (code) && (code) <= KC_EXSEL) | ||||
#define IS_MOD(code) (KC_LCTRL <= (code) && (code) <= KC_RGUI) | #define IS_MOD(code) (KC_LCTRL <= (code) && (code) <= KC_RGUI) | ||||
#define IS_SPECIAL(code) ((0xA5 <= (code) && (code) <= 0xDF) || (0xE8 <= (code) && (code) <= 0xFF)) | |||||
#define IS_SYSTEM(code) (KC_POWER <= (code) && (code) <= KC_WAKE) | |||||
#define IS_CONSUMER(code) (KC_MUTE <= (code) && (code) <= KC_WFAV) | |||||
#define IS_FN(code) (KC_FN0 <= (code) && (code) <= KC_FN31) | #define IS_FN(code) (KC_FN0 <= (code) && (code) <= KC_FN31) | ||||
#define IS_MOUSEKEY(code) (KC_MS_UP <= (code) && (code) <= KC_MS_ACCEL2) | #define IS_MOUSEKEY(code) (KC_MS_UP <= (code) && (code) <= KC_MS_ACCEL2) | ||||
#define IS_MOUSEKEY_MOVE(code) (KC_MS_UP <= (code) && (code) <= KC_MS_RIGHT) | #define IS_MOUSEKEY_MOVE(code) (KC_MS_UP <= (code) && (code) <= KC_MS_RIGHT) | ||||
#define IS_MOUSEKEY_WHEEL(code) (KC_MS_WH_UP <= (code) && (code) <= KC_MS_WH_RIGHT) | #define IS_MOUSEKEY_WHEEL(code) (KC_MS_WH_UP <= (code) && (code) <= KC_MS_WH_RIGHT) | ||||
#define IS_MOUSEKEY_ACCEL(code) (KC_MS_ACCEL0 <= (code) && (code) <= KC_MS_ACCEL2) | #define IS_MOUSEKEY_ACCEL(code) (KC_MS_ACCEL0 <= (code) && (code) <= KC_MS_ACCEL2) | ||||
#define IS_SPECIAL(code) ((0xA5 <= (code) && (code) <= 0xDF) || (0xE8 <= (code) && (code) <= 0xFF)) | |||||
#define IS_CONSUMER(code) (KC_MUTE <= (code) && (code) <= KC_WFAV) | |||||
#define IS_SYSTEM(code) (KC_POWER <= (code) && (code) <= KC_WAKE) | |||||
#define MOD_BIT(code) (1<<MOD_INDEX(code)) | #define MOD_BIT(code) (1<<MOD_INDEX(code)) | ||||
#define MOD_INDEX(code) ((code) & 0x07) | #define MOD_INDEX(code) ((code) & 0x07) | ||||
#define FN_BIT(code) (1<<FN_INDEX(code)) | #define FN_BIT(code) (1<<FN_INDEX(code)) | ||||
#define KC_WSTP KC_WWW_STOP | #define KC_WSTP KC_WWW_STOP | ||||
#define KC_WREF KC_WWW_REFRESH | #define KC_WREF KC_WWW_REFRESH | ||||
#define KC_WFAV KC_WWW_FAVORITES | #define KC_WFAV KC_WWW_FAVORITES | ||||
/* Transparent */ | |||||
#define KC_TRANSPARENT 1 | |||||
#define KC_TRNS KC_TRANSPARENT | |||||
/* USB HID Keyboard/Keypad Usage(0x07) */ | /* USB HID Keyboard/Keypad Usage(0x07) */ |
#include "keymap.h" | #include "keymap.h" | ||||
#include "report.h" | #include "report.h" | ||||
#include "keycode.h" | #include "keycode.h" | ||||
#include "action.h" | |||||
/* layer */ | /* layer */ | ||||
uint8_t current_layer = 0; | uint8_t current_layer = 0; | ||||
#ifndef NO_LEGACY_KEYMAP_SUPPORT | |||||
/* legacy support with weak reference */ | |||||
__attribute__ ((weak)) | |||||
action_t keymap_get_action(uint8_t layer, uint8_t row, uint8_t col) | |||||
action_t keymap_keycode_to_action(uint8_t keycode) | |||||
{ | { | ||||
/* convert from legacy keycode to action */ | |||||
uint8_t key = keymap_get_keycode(layer, row, col); | |||||
action_t action; | action_t action; | ||||
switch (key) { | |||||
switch (keycode) { | |||||
case KC_A ... KC_EXSEL: | case KC_A ... KC_EXSEL: | ||||
action.code = ACTION_KEY(key); | |||||
action.code = ACTION_KEY(keycode); | |||||
break; | break; | ||||
case KC_LCTRL ... KC_LGUI: | case KC_LCTRL ... KC_LGUI: | ||||
action.code = ACTION_LMOD(key); | |||||
action.code = ACTION_LMOD(keycode); | |||||
break; | break; | ||||
case KC_RCTRL ... KC_RGUI: | case KC_RCTRL ... KC_RGUI: | ||||
action.code = ACTION_RMOD(key); | |||||
action.code = ACTION_RMOD(keycode); | |||||
break; | break; | ||||
case KC_SYSTEM_POWER ... KC_SYSTEM_WAKE: | case KC_SYSTEM_POWER ... KC_SYSTEM_WAKE: | ||||
action.code = ACTION_USAGE_SYSTEM(KEYCODE2SYSTEM(key)); | |||||
action.code = ACTION_USAGE_SYSTEM(KEYCODE2SYSTEM(keycode)); | |||||
break; | break; | ||||
case KC_AUDIO_MUTE ... KC_WWW_FAVORITES: | case KC_AUDIO_MUTE ... KC_WWW_FAVORITES: | ||||
action.code = ACTION_USAGE_CONSUMER(KEYCODE2CONSUMER(key)); | |||||
action.code = ACTION_USAGE_CONSUMER(KEYCODE2CONSUMER(keycode)); | |||||
break; | break; | ||||
case KC_MS_UP ... KC_MS_ACCEL2: | case KC_MS_UP ... KC_MS_ACCEL2: | ||||
action.code = ACTION_MOUSEKEY(key); | |||||
action.code = ACTION_MOUSEKEY(keycode); | |||||
break; | break; | ||||
case KC_FN0 ... KC_FN31: | |||||
{ | |||||
uint8_t layer = keymap_fn_layer(FN_INDEX(key)); | |||||
uint8_t code = keymap_fn_keycode(FN_INDEX(key)); | |||||
action.code = ACTION_LAYER_SET_TAP_KEY(layer, code); | |||||
} | |||||
case KC_TRNS: | |||||
action.code = ACTION_TRANSPARENT; | |||||
break; | break; | ||||
case KC_NO ... KC_UNDEFINED: | |||||
default: | default: | ||||
action.code = ACTION_NO; | action.code = ACTION_NO; | ||||
break; | break; | ||||
} | } | ||||
return action; | return action; | ||||
} | } | ||||
#ifndef NO_LEGACY_KEYMAP_SUPPORT | |||||
/* legacy support with weak reference */ | |||||
__attribute__ ((weak)) | |||||
action_t keymap_get_action(uint8_t layer, uint8_t row, uint8_t col) | |||||
{ | |||||
/* convert from legacy keycode to action */ | |||||
uint8_t keycode = keymap_get_keycode(layer, row, col); | |||||
action_t action; | |||||
switch (keycode) { | |||||
case KC_FN0 ... KC_FN31: | |||||
{ | |||||
uint8_t layer = keymap_fn_layer(FN_INDEX(keycode)); | |||||
uint8_t key = keymap_fn_keycode(FN_INDEX(keycode)); | |||||
if (key) { | |||||
action.code = ACTION_LAYER_SET_TAP_KEY(layer, key); | |||||
} else { | |||||
action.code = ACTION_LAYER_SET_MOMENTARY(layer); | |||||
} | |||||
} | |||||
return action; | |||||
default: | |||||
return keymap_keycode_to_action(keycode); | |||||
} | |||||
} | |||||
#endif | #endif | ||||
__attribute__ ((weak)) | __attribute__ ((weak)) |
extern uint8_t default_layer; | extern uint8_t default_layer; | ||||
/* translates key_t to keycode */ | |||||
uint8_t keymap_key_to_keycode(uint8_t layer, key_t key); | |||||
/* translates keycode to action */ | |||||
action_t keymap_keycode_to_action(uint8_t keycode); | |||||
/* translates Fn keycode to action */ | |||||
action_t keymap_fn_to_action(uint8_t keycode); | |||||
/* action for key */ | /* action for key */ | ||||
// TODO: should use struct key_t? | |||||
// TODO: should use struct key_t? move to action.h? | |||||
action_t keymap_get_action(uint8_t layer, uint8_t row, uint8_t col); | action_t keymap_get_action(uint8_t layer, uint8_t row, uint8_t col); | ||||
/* user defined special function */ | /* user defined special function */ | ||||
void keymap_call_function(keyrecord_t *record, uint8_t id, uint8_t opt); | void keymap_call_function(keyrecord_t *record, uint8_t id, uint8_t opt); | ||||
#ifndef NO_LEGACY_KEYMAP_SUPPORT | #ifndef NO_LEGACY_KEYMAP_SUPPORT | ||||
/* keycode of key */ | /* keycode of key */ | ||||
uint8_t keymap_get_keycode(uint8_t layer, uint8_t row, uint8_t col); | uint8_t keymap_get_keycode(uint8_t layer, uint8_t row, uint8_t col); |