← More about keyboards

This post describes a QMK macro “Mouse Turbo Click” that clicks the mouse rapidly:

Turbo Click may be advantageous in certain computer games. It could also be used in art programs to paint in a dashed pattern. I don’t know that there is a “serious” use for Turbo Click, but it is interesting how auto-repeating actions like this can be implemented in QMK.

Add it to your keymap

If you are new to QMK macros, see my macro buttons post for an intro.

Step 1: In your keymap.c, add a custom keycode for activating Turbo Click and use the new keycode somewhere in your layout. I’ll name it TURBO, but you can call it anything you like.

enum custom_keycodes {
  TURBO = SAFE_RANGE,
  // Other custom keys...
};

Step 2: Handle Turbo Click from your process_record_user() function by calling process_mouse_turbo_click(), passing your custom keycode as the third argument:

#include "features/mouse_turbo_click.h"

bool process_record_user(uint16_t keycode, keyrecord_t* record) {
  if (!process_mouse_turbo_click(keycode, record, TURBO)) { return false; }
  // Your macros ...

  return true;
}

Step 3: In your rules.mk file, add

SRC += features/mouse_turbo_click.c

MOUSEKEY_ENABLE = yes
DEFERRED_EXEC_ENABLE = yes

Step 4: In the directory containing your keymap.c, create a features subdirectory and copy mouse_turbo_click.h and mouse_turbo_click.c there.

Customization

Click period

The clicking speed is controlled by MOUSE_TURBO_CLICK_PERIOD, set to a period of 80 milliseconds by default for 1000 / 80 = 12.5 clicks per second. To change the click rate, define MOUSE_TURBO_CLICK_PERIOD in config.h like

// Click every 200 ms for 5 clicks per second.
#define MOUSE_TURBO_CLICK_PERIOD 200

A smaller period implies faster clicking. Beyond some point, it is conceivable that QMK or the application running on the computer is overwhelmed with handling the rapid mouse clicks. I suggest setting the period no smaller than 10 ms or hundred clicks per second.

Click key

By default, Turbo Click clicks the first mouse button KC_MS_BTN1. To click a different button, define MOUSE_TURBO_CLICK_KEY in config.h. For instance, you could spam the Space key instead with

// Repeatedly click the Space key.
#define MOUSE_TURBO_CLICK_KEY KC_SPC

MOUSE_TURBO_CLICK_KEY may be set to any basic keycode or 16-bit keycode that works with register_code16(), which includes at least modifier + basic key chords. If set to a non-mouse key, MOUSEKEY_ENABLE = yes is no longer required.

Explanation

The implementation uses mouse keys and a periodic callback using the deferred execution API. When Turbo Click starts, turbo_click_callback() is scheduled. The callback executes twice per click, alternating between pressing and releasing MOUSE_TURBO_CLICK_KEY. When Turbo Click stops, the callback is canceled and the key is released if needed.

← More about keyboards