Gama C Library
Gama C API Documentation
key.h
Go to the documentation of this file.
1#pragma once
2
3/**
4 * @brief Converts a character to lowercase if it's an uppercase letter.
5 * @param k The character to convert.
6 * @return The lowercase version of the character, or the original if not
7 * uppercase.
8 */
9char gm_lower_case(char k) {
10 int i = (int)k;
11 return (i >= (int)'A' && i <= (int)'Z') ? (char)(i + (int)'a' - (int)'A') : k;
12}
13
14/**
15 * @brief Converts a character to uppercase if it's a lowercase letter.
16 * @param k The character to convert.
17 * @return The uppercase version of the character, or the original if not
18 * lowercase.
19 */
20char gm_upper_case(char k) {
21 int i = (int)k;
22 return (i >= (int)'a' && i <= (int)'z') ? (char)(i + (int)'A' - (int)'a') : k;
23}
24
25/**
26 * @brief Decodes a single character shortcut into a key type and key code.
27 *
28 * This function translates a compact shortcut character into its corresponding
29 * event type and key, used by `gm_key_down`.
30 * - 'U', 'D', 'L', 'R' -> type 'a' (arrow)
31 * - 'E' -> type 's' (special), key 'x'
32 * - 'S', 'C', 'A' -> type 'm' (modifier: Shift, Ctrl, Alt)
33 * - Other characters -> type 'c' (character)
34 *
35 * @param key The single character shortcut (e.g., 'U' for Up Arrow).
36 * @param t Pointer to store the decoded key type.
37 * @param k Pointer to store the decoded key code.
38 */
39void gm_decode_key_shortcut(char key, char *t, char *k) {
40 switch (key) {
41 case '\0':
42 *t = ' ';
43 *k = ' ';
44 break;
45 case 'U':
46 case 'D':
47 case 'L':
48 case 'R':
49 *t = 'a';
50 *k = gm_lower_case(key);
51 break;
52 case 'E':
53 *t = 's';
54 *k = 'x';
55 break;
56 case 'S':
57 case 'C':
58 case 'A':
59 *t = 'm';
60 *k = gm_lower_case(key);
61 break;
62 default:
63 *t = 'c';
64 *k = key;
65 }
66}
67
68/**
69 * @brief Encodes a key type and code into a single character shortcut.
70 * This is the reverse of `gm_decode_key_shortcut`.
71 * @param t The key type ('a' for arrow, 'm' for modifier, 's' for special, 'c' for character).
72 * @param k The key code.
73 * @return The encoded single character shortcut.
74 */
75char gm_encode_key_shortcut(char t, char k) {
76 switch (t) {
77 case ' ':
78 return '\0';
79 case 'a':
80 case 'm':
81 return gm_upper_case(k);
82 case 's':
83 switch (k) {
84 case 'x':
85 return 'E';
86 }
87 case 'c':
88 return k;
89 default:
90 return '\0';
91 }
92}
93
94#ifndef GM_NO_GAPI
95#include "gapi.h"
96
97/**
98 * @brief Checks if a key is currently pressed.
99 *
100 * This is the low-level input checking function.
101 *
102 * @param t The type of key: 'c' for character, 'a' for arrow, 's' for special.
103 * @param k The key code to check (e.g., 'w', 'u' for up arrow).
104 * @return 1 if the key is pressed, 0 otherwise.
105 */
106int gm_key_down(char t, char k) { return gapi_key_down(t, k); }
107
108/**
109 * @brief Checks if a key is currently pressed using a single character shortcut.
110 * @param key The shortcut character to check (e.g., 'w', 'U' for up).
111 * @return 1 if the key is pressed, 0 otherwise.
112 * @see gm_decode_key_shortcut
113 * @see gm_key_down
114 */
115int gm_key(char key) {
116 char t, k;
117 gm_decode_key_shortcut(key, &t, &k);
118 return gm_key_down(t, k);
119}
120
121#endif
Graphics API (GAPI) abstraction layer for Gama.
int32_t gapi_key_down(char t, char k)
Checks if a specific key is currently pressed.
char gm_upper_case(char k)
Converts a character to uppercase if it's a lowercase letter.
Definition key.h:20
void gm_decode_key_shortcut(char key, char *t, char *k)
Decodes a single character shortcut into a key type and key code.
Definition key.h:39
int gm_key_down(char t, char k)
Checks if a key is currently pressed.
Definition key.h:106
char gm_encode_key_shortcut(char t, char k)
Encodes a key type and code into a single character shortcut. This is the reverse of gm_decode_key_sh...
Definition key.h:75
char gm_lower_case(char k)
Converts a character to lowercase if it's an uppercase letter.
Definition key.h:9
int gm_key(char key)
Checks if a key is currently pressed using a single character shortcut.
Definition key.h:115