zmk icon indicating copy to clipboard operation
zmk copied to clipboard

feat(behaviors): Add a send string behavior

Open joelspadin opened this issue 1 year ago • 15 comments

This adds the following:

  • A character map driver API, which maps Unicode code points to behavior bindings.
  • A zmk,character-map driver which implements this API. This driver is designed for ROM efficiency, and it sends every value defined in the map to one behavior and passes any code point not in the map through to another. (A more flexible implementation that allows for a unique behavior binding per character could be added later if necessary.)
  • A zmk,send-string behavior, which users can define and bind to their keymaps to send strings.
  • A zmk_send_string() function, which queues the necessary behaviors to type a UTF-8 string. This is separated from the send string behavior since it could be used for other features such as Unicode key sequences, behaviors that print dynamic messages, etc.

joelspadin avatar Aug 06 '23 04:08 joelspadin

I will do a second pass later but the docs look great, thank you for the effort you put into it! One suggestion I'd have is to put a pointer in the macros page pointing to this behavior for string sending purposes, similar to the pointer we have for modifier functions.

caksoylar avatar Aug 06 '23 05:08 caksoylar

I've been waiting for this function. If there is a printf-style format function, I would be more than happy. Once I read the zephyr's code related to printf, It's difficult to understand to me.

jhorology avatar Aug 06 '23 09:08 jhorology

One suggestion I'd have is to put a pointer in the macros page pointing to this behavior for string sending purposes, similar to the pointer we have for modifier functions.

Good idea. I'll add that.

I've been waiting for this function. If there is a printf-style format function, I would be more than happy. Once I read the zephyr's code related to printf, It's difficult to understand to me.

You could use the zmk_send_string() function with printf by printing to a string buffer and then sending that to the function, for example:

struct zmk_send_string_config config = {
    .character_map = DEVICE_DT_GET(DT_CHOSEN(zmk_character_map)),
    .wait_ms = CONFIG_ZMK_SEND_STRING_DEFAULT_WAIT_MS,
    .tap_ms = CONFIG_ZMK_SEND_STRING_DEFAULT_TAP_MS,
};

char string[32];
snprintf(string, sizeof(string), "Battery level is %u%%", zmk_battery_state_of_charge());

zmk_send_string(&config, position, string);

joelspadin avatar Aug 06 '23 15:08 joelspadin

I added some macros for creating the config structs, so the code example above can now be simplified to

ZMK_BUILD_ASSERT_CHARACTER_MAP_CHOSEN();
...

char string[32];
snprintf(string, sizeof(string), "Battery level is %u%%", zmk_battery_state_of_charge());

zmk_send_string(&ZMK_SEND_STRING_CONFIG_DEFAULT, position, string);

joelspadin avatar Aug 07 '23 15:08 joelspadin

LGTM from a docs perspective :+1:

caksoylar avatar Sep 03 '23 22:09 caksoylar

pr broken because of this? https://github.com/zmkfirmware/zmk/commit/690bc1bb44b1b62228900906cb308dc6f1a05eb8

minhe7735 avatar Nov 07 '23 02:11 minhe7735

Will be great if this gets merged!

dryqin avatar Dec 15 '23 23:12 dryqin

Rebased and fixed everything to work with current ZMK.

joelspadin avatar Mar 21 '24 04:03 joelspadin

I've been eagerly waiting for this for almost a year now. Is there any chance we'll get to see this merged soon? Thank you!

prez avatar May 27 '24 10:05 prez