libghostty
Loading...
Searching...
No Matches
c-vt-key-encode/src/main.c

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>
#include <ghostty/vt.h>
int main() {
GhosttyResult result = ghostty_key_encoder_new(NULL, &encoder);
assert(result == GHOSTTY_SUCCESS);
// Set kitty flags with all features enabled
// Create key event
result = ghostty_key_event_new(NULL, &event);
assert(result == GHOSTTY_SUCCESS);
ghostty_key_event_set_key(event, GHOSTTY_KEY_CONTROL_LEFT);
printf("Encoding event: left ctrl release with all Kitty flags enabled\n");
// Optionally, encode with null buffer to get required size. You can
// skip this step and provide a sufficiently large buffer directly.
// If there isn't enoug hspace, the function will return an out of memory
// error.
size_t required = 0;
result = ghostty_key_encoder_encode(encoder, event, NULL, 0, &required);
assert(result == GHOSTTY_OUT_OF_MEMORY);
printf("Required buffer size: %zu bytes\n", required);
// Encode the key event. We don't use our required size above because
// that was just an example; we know 128 bytes is enough.
char buf[128];
size_t written = 0;
result = ghostty_key_encoder_encode(encoder, event, buf, sizeof(buf), &written);
assert(result == GHOSTTY_SUCCESS);
printf("Encoded %zu bytes\n", written);
// Print the encoded sequence (hex and string)
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
Definition encoder.h:56
#define GHOSTTY_MODS_CTRL
Definition event.h:61
void ghostty_key_event_set_mods(GhosttyKeyEvent event, GhosttyMods mods)
struct GhosttyKeyEncoder * GhosttyKeyEncoder
Definition encoder.h:24
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
Definition event.h:24
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
Definition encoder.h:102
@ GHOSTTY_KEY_ACTION_RELEASE
Definition event.h:33
GhosttyResult
Definition result.h:13
@ GHOSTTY_OUT_OF_MEMORY
Definition result.h:17
@ GHOSTTY_SUCCESS
Definition result.h:15