0

I want to change octaves or transpositions when turning the knob on my keyboard. What could I do to get the following code to work?

typedef enum {
ENCODER_MODE_OCTAVE,
ENCODER_MODE_TRANSPOSE,
} encoder_mode_t;

encoder_mode_t encoder_mode = ENCODER_MODE_OCTAVE;

bool encoder_update_user(uint8_t index, bool clockwise) {
    if (layer_state_is(MIDI_BASE)) {
        if (clockwise) {
            if (encoder_mode == ENCODER_MODE_OCTAVE) {
                tap_code16(MI_OCTU);
            } else {
                tap_code16(MI_TRSU);
            }
        } else {
            if (encoder_mode == ENCODER_MODE_OCTAVE) {
                tap_code16(MI_OCTD);
            } else {
                tap_code16(MI_TRSD);
            }
        }
    }
    return false;
}

  • I receive 'something' when turning the knob, but it's not MI_OCTx or MI_TRSx.
  • The documentation specifies tap_code16(<kc>); so I'm thinking that I can only send KC_x keycodes, but I am unsure.
  • Using MI_OCTx or MI_TRSxin my keymap works.
  • As a last option, I could implement octave and transposition changes in process_record_user by adding or subtracting from the midi note values before using midi_send_noteon, but I am hoping for a 'simpler' solution.
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Max May
  • 13
  • 4

1 Answers1

0

You are correct. MIDI events and real keyboard events are processed separately: OCTx and TRSx are processed in process_midi, which is part of the process that regular keymap goes through, but not part of tap_code*. Moreover, those OCTx/TRSx and other MIDI functions are not exposed for users to call.

I do have an idea, but haven't looked into this myself, so please take it as a grain of salt.

The goal here is to utilize those internal MIDI functions that are already in process_midi, and we want to be able to do this from process_record_user (or pre_process_record_user). The idea is to change the content of *record on the fly so the keycode becomes MI_OCTx. When it reaches process_midi, it'll do what MI_OCTx is supposed to do. I expect you'll need to do some (un)register_code to keep the queue clean.

Tsan-Kuang Lee
  • 1,454
  • 1
  • 14
  • 10