How can I cause a synthetic autoshift keypress in QMK (for the Moonlander)?

ajrskelton

I written some code to indicate that the numlock is active by changing its LED colour, like so:

bool process_record_user(uint16_t keycode, keyrecord_t *record) {
  switch (keycode) {
    case TOG_NUMS:
      if (record->event.pressed) {
        tap_code(KC_NUM_LOCK); // Synthetic Numlock keypress
        isToggle[IND_NUMS] = !isToggle[IND_NUMS]; // Toggle the indicator
        set_toggle_indicator(IND_NUMS); // Change LED colour based on isToggle value
      }
      return true;
    default:
      return true;
  }
  return true;
}

This works as expected: The custom keycode TOG_NUMS toggles the numpad by calling tap_code and makes a call to my set_toggle_indicator function to update the ledmap. When I try the same thing with my custom autoshift key, however, I run into issues. This is what I did to try and follow the same pattern:

  case TOG_ASHF:
    if (record->event.pressed) {
      tap_code(KC_ASTG);
      isToggle[IND_ASHF] = !isToggle[IND_ASHF];
      set_toggle_indicator(IND_ASHF);
    }
    return true;

The above doesn't compile, however, since the type of the KC_ASTG keycode is integer, while the tap_code parameter is an unsigned char. The compiler gives me this error message:

error: unsigned conversion from 'int' to 'uint8_t' {aka 'unsigned char'} changes value from '23578' to '26'

If I replace tap_code with tap_code16, my code compiles, but the key behaves strangely. It appears to simply give me a lowercase w when pressed. How can I get the KC_ASTG keypress to trigger without using tapcode?

ajrskelton

I figured it out! A synthetic keypress was the wrong approach. The KC_ASTG keycode does not have a matching scancode like the numpad key does. Instead of sending a scancode to the OS via USB, the logic is handled in the process_auto_shift middleware. I included process_auto_shift.h into my keymap file and copied the implementation of the middleware into the TOG_ASHF case statement, like so:

case TOG_ASHF:
      if (record->event.pressed) {
        autoshift_toggle();
        isToggle[IND_ASHF] = !isToggle[IND_ASHF];
        set_toggle_indicator(IND_ASHF);
      }
      return true;

It behaves as expected.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I simulate a keypress in JavaScript?

How can i make a keypress action to this code?

how can i mute an iframe on keypress

How can I call a already defined function on keypress?

How can I position my cursor in an input field based on a keypress?

How can I pass a keypress value to a function, through an event listener?

How can I use keypress and prompt libraries same time?

How can I broadcast and pick up a keypress event in AngularJS?

How can I get jquery .val() AFTER keypress event?

How can I listen for keypress event on the whole page?

How can I use python xlib to generate a single keypress?

How can I fire the Tab keypress when the Enter key is pressed?

How can i count the number of keypress in the background in C#

How can I get around a keypress requirement on a javascript textbox?

How can I cause a "initscr" break with ncurses?

How can I fix This error and what is the cause of it?

How can I cause Python 3.5 to crash?

How can i find issue cause in git?

How can I programmatically cause a delay in Android?

How can I avoid synthetic access compiler warning with inline-anonymous class declaration and what does it mean?

How can I register only numbers in TextBox?(i dont have KeyPress event)

Can I bind action to a keypress in Emacs?

How can I cause a child process to exit when the parent does?

How can I pass a collection of exceptions as a root cause?

How can I find out if an update cause a change in SQLITE with Xamarin?

How can I make this F# function not cause a stack overflow

How can I get format to not cause a type-hinting error?

How can I check if multiplying two numbers in Java will cause an overflow?

In React, how can I cause anchors to do nothing on click?