Skip to content

Keyboard inject_switch

HASUMI Hitoshi edited this page Aug 12, 2022 · 6 revisions

Valid version

0.9.17+

Usage

Keyboard#inject_switch will inject a switch position in the keymap as if it was tapped:

kbd.inject_switch(0, 1)
#                 ^  ^col position
#                 |
#                 row position

Let's say you have a 1x4 matrix macro pad that has a rotary encoder:

kbd.init_pins(
  [1],
  [10, 11, 12, 13]
)
kbd.add_layer :default, %i[
  KC_LEFT KC_DOWN KC_UP KC_RIGHT
]
encoder = RotaryEncoder.new(15, 16)
encoder.clockwise do
  kbd.send_key :KC_VOLU
end
encoder.counterclockwise do
  kbd.send_key :KC_VOLD
end
kbd.append encoder

It works. But you can also use inject_switch with "vacant" matrix:

 kbd.init_pins(
-  [1],
+  [1, 2], # (*)
   [10, 11, 12, 13]
 )
 kbd.add_layer :default, %i[
   KC_LEFT KC_DOWN KC_UP KC_RIGHT
+  KC_VOLU KC_VOLD KC_NO KC_NO
 ]
 encoder = RotaryEncoder.new(15, 16)
 encoder.clockwise do
-  kbd.send_key :KC_VOLU
+  kbd.inject_switch(1, 0)
 end
 encoder.counterclockwise do
-  kbd.send_key :KC_VOLD
+  kbd.inject_switch(1, 1)
 end
 kbd.append encoder

The advantage of this method is that you can re-assign keycodes to the encoder by VIA/Remap.

See also Rotary encoder and VIA and Remap.


Note: (*) GPIO 2 ideally should be connected to GND via a resistor though, it generally can be just floated.

Clone this wiki locally