This example demonstrates how to use the key encoder to convert key events into terminal escape sequences using the Kitty keyboard protocol.
#include <assert.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
int main() {
printf("Encoding event: left ctrl release with all Kitty flags enabled\n");
size_t required = 0;
printf("Required buffer size: %zu bytes\n", required);
char buf[128];
size_t written = 0;
printf("Encoded %zu bytes\n", written);
printf("Hex: ");
for (size_t i = 0; i < written; i++) printf("%02x ", (unsigned char)buf[i]);
printf("\n");
printf("String: ");
for (size_t i = 0; i < written; i++) {
if (buf[i] == 0x1b) {
printf("\\x1b");
} else {
printf("%c", buf[i]);
}
}
printf("\n");
return 0;
}
#define GHOSTTY_KITTY_KEY_ALL
#define GHOSTTY_MODS_CTRL
void ghostty_key_event_set_mods(GhosttyKeyEvent event, GhosttyMods mods)
struct GhosttyKeyEncoder * GhosttyKeyEncoder
void ghostty_key_event_set_action(GhosttyKeyEvent event, GhosttyKeyAction action)
GhosttyResult ghostty_key_encoder_new(const GhosttyAllocator *allocator, GhosttyKeyEncoder *encoder)
GhosttyResult ghostty_key_event_new(const GhosttyAllocator *allocator, GhosttyKeyEvent *event)
void ghostty_key_encoder_setopt(GhosttyKeyEncoder encoder, GhosttyKeyEncoderOption option, const void *value)
struct GhosttyKeyEvent * GhosttyKeyEvent
GhosttyResult ghostty_key_encoder_encode(GhosttyKeyEncoder encoder, GhosttyKeyEvent event, char *out_buf, size_t out_buf_size, size_t *out_len)
void ghostty_key_encoder_free(GhosttyKeyEncoder encoder)
void ghostty_key_event_free(GhosttyKeyEvent event)
void ghostty_key_event_set_key(GhosttyKeyEvent event, GhosttyKey key)
@ GHOSTTY_KEY_ENCODER_OPT_KITTY_FLAGS
@ GHOSTTY_KEY_ACTION_RELEASE