Browse Source

Merge pull request #321 from njbair/master

Clarify and clean up wording in keymap.md, section 0
master
tmk 8 years ago
parent
commit
79afcd5570
1 changed files with 55 additions and 33 deletions
  1. 55
    33
      tmk_core/doc/keymap.md

+ 55
- 33
tmk_core/doc/keymap.md View File

@@ -3,9 +3,9 @@ Keymap framework - how to define your keymap
***NOTE: This is not final version, may be inconsistent with source code and changed occasionally for a while.***

## 0. Keymap and layers
**Keymap** is comprised of multiple layers of key layout, you can define **32 layers** at most.
**Layer** is an array of **keycodes** to define **actions** for each physical keys.
respective layers can be validated simultaneously. Layers are indexed with 0 to 31 and higher layer has precedence.
The **keymap** is an array composed of one or more layers.
Each **layer** is an array of **keycodes**, defining **actions** for each physical key.
Layers can be activated and deactivated independently. Multiple layers may be active at once, resulting in the currently-active **layer state**. Each layer has an index between 0-31. As active layers are stacked together, higher layers take precedence over lower layers.

Keymap: 32 Layers Layer: Keycode matrix
----------------- ---------------------
@@ -21,31 +21,17 @@ respective layers can be validated simultaneously. Layers are indexed with 0 to
1 /___________// | 1 `--------------------------
0 /___________/ V low 0 `--------------------------

**Note:** The keymap array is limited to **32 layers**.


### 0.1 Keymap status
Keymap has its state in two parameters:
**`default_layer`** indicates a base keymap layer(0-31) which is always valid and to be referred, **`keymap_stat`** is 16bit variable which has current on/off status of layers on its each bit.

Keymap layer '0' is usually `default_layer` and which is the only valid layer and other layers is initially off after boot up firmware, though, you can configured them in `config.h`.
To change `default_layer` will be useful when you switch key layout completely, say you want Colmak instead of Qwerty.
### 0.1 Layer state
The current keymap layer state is determined by two parameters: the *default layer*, and the individual *layer states*. Changing the default layer is useful for switching key layouts completely; for example, switching to Dvorak, Colemak or Workman instead of QWERTY. Individual layer states, on the other hand, can be used to overlay the base layer with other functions such as navigation keys, function keys (F1-F12), media keys or other actions.

Initial state of Keymap Change base layout
----------------------- ------------------
Because the default layer is really just a special case affecting the overall layer state, it is important to first understand how the layer state is determined.

31 31
30 30
29 29
: :
: : ____________
2 ____________ 2 / /
1 / / ,->1 /___________/
,->0 /___________/ | 0
| |
`--- default_layer = 0 `--- default_layer = 1
layer_state = 0x00000001 layer_state = 0x00000002

On the other hand, you shall change `layer_state` to overlay base layer with some layers for feature such as navigation keys, function key(F1-F12), media keys or special actions.
#### 0.1.1 The layer state
The **layer state** indicates the current on/off status of all layers. It is defined in the firmware by a 32-bit integer, `layer_state`, which stores each layer's on/off status in a single bit: 0 for off, 1 for on. As layers are activated and deactivated, their respective bits are flipped, changing the value of `layer_state`.

Overlay feature layer
--------------------- bit|status
@@ -62,28 +48,64 @@ On the other hand, you shall change `layer_state` to overlay base layer with som
`--- default_layer = 1 |
layer_state = 0x60000002 <-'

#### 0.1.2 The default layer
The **default layer** is the base keymap layer (0-31) which is always active and considered the "bottom" of the stack. When the firmware boots, the default layer is the only active layer. It is set to layer 0 by default, though this can be changed ~~in *config.h*~~ via Boot Magic settings.

Initial state of Keymap Change base layout
----------------------- ------------------

31 31
30 30
29 29
: :
: : ____________
2 ____________ 2 / /
1 / / ,->1 /___________/
,->0 /___________/ | 0
| |
`--- default_layer = 0 `--- default_layer = 1
layer_state = 0x00000001 layer_state = 0x00000002

Note that the `default_layer_state` variable only determines the lowest value to which `layer_state` may be set, and that `default_layer_state` is used by the core firmware when determining the starting value of `layer_state` before applying changes. In other words, the default layer will *always* be set to *on* in `layer_state`.

The default layer is defined in the firmware by the `default_layer_state` variable, which is identical in format to the `layer_state` variable exlpained above. The value may be changed using the following functions:

- `default_layer_state_set(state)` sets the state to the specified 32-bit integer value.
- AND/OR/XOR functions set the state based on a boolean logic comparison between the current state and the specified 32-bit integer value:
- `default_layer_state_and(state)`
- `default_layer_state_or(state)`
- `default_layer_state_xor(state)`

For example, to set layer 3 as the default layer:

```C
// convert 3 to a 32-bit unsigned long value, and set the default layer
default_layer_state_set(1UL<<3);
```



### 0.2 Layer Precedence and Transparency
Note that ***higher layer has higher priority on stack of layers***, namely firmware falls down from top layer to bottom to look up keycode. Once it spots keycode other than **`KC_TRNS`**(transparent) on a layer it stops searching and lower layers aren't referred.
Note that ***higher layers have priority in the layer stack***. The firmware starts at the topmost active layer, and works down to the bottom to find the an active keycode. Once the search encounters any keycode other than **`KC_TRNS`** (transparent) on an active layer, the search is halted and the remaining lower layers aren't examined, even if they are active.

**Note:** a layer must be activated before it may be included in the stack search.

`KC_TRNS` is a special placeholder which can be used on overlay layers. This allows for the creation of "partial" layers which fall back on the lower layers, eliminating a good deal of repetition in keymap files.

You can place `KC_TRNS` on overlay layer changes just part of layout to fall back on lower or base layer.
Key with `KC_TRANS` doesn't has its own keycode and refers to lower valid layers for keycode, instead.
See example below.


### 0.3 Keymap Example
Keymap is **`keymaps[]`** C array in fact and you can define layers in it with **`KEYMAP()`** C macro and keycodes. To use complex actions you need to define `Fn` keycode in **`fn_actions[]`** array.
The keymap is defined in the **`keymaps[]`** array, a 2-dimensional array of rows and columns corresponding to positions in the keyboard matrix. But most often the layers are defined using C macros to allow for easier reading and editing of the keymap files. To use complex actions you need to define `Fn` keycodes in the **`fn_actions[]`** array.

This is a keymap example for [HHKB](http://en.wikipedia.org/wiki/Happy_Hacking_Keyboard) keyboard.
This example has three layers, 'Qwerty' as base layer, 'Cursor' and 'Mousekey'.
This is a keymap example for the [HHKB](http://en.wikipedia.org/wiki/Happy_Hacking_Keyboard) keyboard.
This example has three layers: the QWERTY base layer, and two overlay layers for cursor and mousekey control, respectively.
In this example,

`Fn0` is a **momentary layer switching** key, you can use keys on Cursor layer while holding the key.
`Fn0` is a **momentary layer switching** key--you can use keys on the Cursor layer while holding the key.

`Fn1` is a momentary layer switching key with tapping feature, you can get semicolon **';'** with taping the key and switch layers while holding the key. The word **'tap'** or **'tapping'** mean to press and release a key quickly.
`Fn1` is a momentary layer switching key with tapping function--tapping the key as one would normally use it, sends the semicolon **';'** keycode, while holding the key down switches layers.

`Fn2` is a **toggle layer switch** key, you can stay switched layer after releasing the key unlike momentary switching.
`Fn2` is a **toggle layer switch** key--pressing the key toggles the layer on until you press it again.

You can find other keymap definitions in file `keymap.c` located on project directories.