libghostty
Loading...
Searching...
No Matches
c-vt-encode-key/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() {
// Create encoder
GhosttyResult result = ghostty_key_encoder_new(NULL, &encoder);
assert(result == GHOSTTY_SUCCESS);
// Enable Kitty keyboard protocol with all features
&(uint8_t){GHOSTTY_KITTY_KEY_ALL});
// Create and configure key event for Ctrl+C press
result = ghostty_key_event_new(NULL, &event);
assert(result == GHOSTTY_SUCCESS);
ghostty_key_event_set_key(event, GHOSTTY_KEY_C);
// Encode the key event
char buf[128];
size_t written = 0;
result = ghostty_key_encoder_encode(encoder, event, buf, sizeof(buf), &written);
assert(result == GHOSTTY_SUCCESS);
// Use the encoded sequence (e.g., write to terminal)
fwrite(buf, 1, written, stdout);
// Cleanup
return 0;
}
void ghostty_key_event_set_mods(GhosttyKeyEvent event, GhosttyMods mods)
struct GhosttyKeyEncoder * GhosttyKeyEncoder
Definition key/encoder.h:25
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 key/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
@ GHOSTTY_KEY_ACTION_PRESS
Definition key/event.h:35
#define GHOSTTY_KITTY_KEY_ALL
Definition key/encoder.h:57
#define GHOSTTY_MODS_CTRL
Definition key/event.h:61
GhosttyResult
Definition types.h:13
@ GHOSTTY_SUCCESS
Definition types.h:15