key(3alleg4)

✨ key β€” bestie, it's the keyboard state array ✨

okay so you're reading the Allegro manual page for key(3alleg4) and honestly? it's giving "little array, big power" πŸ’…


🌸 NAME 🌸

key β€” an array of flags that tells you which keys are pressed. It's part of the Allegro game programming library. πŸŽ€


πŸ¦‹ SYNOPSIS πŸ¦‹

#include <allegro.h>

extern volatile char key[KEY_MAX];

πŸ’– DESCRIPTION πŸ’–

okay so bestie, key is literally just an array of flags. each one tells you the state of a key, and the array is ordered by scancode. so like, key[0] is the first scancode, and so on down the line. slay. ✨

wherever possible, these values get updated asynchronously β€” like, they just do their thing in the background, no cap. BUT (and this is the important part, don't skip it πŸ™ˆ) if keyboard_needs_poll() returns TRUE, you have to manually call poll_keyboard() yourself to refresh them with the current input state. no auto-update, you're the main character now, you do it. πŸ‘‘

the scancodes are defined in allegro/keyboard.h as a whole series of KEY_* constants (and yes, they're also listed right down below, i got you 🫢). for example, you could write something like:

if (key[KEY_SPACE])
   printf("Space is pressed\n");

so cute, so simple, i'm obsessed. πŸ’–

note that this array is supposed to represent which keys are physically held down and which ones aren't β€” it's a literal snapshot of your fingers on the keyboard, so it is semantically read-only. meaning: don't go writing into it, bestie, that's not the vibe. it's read-only for a reason. ⚠️

these are the keyboard scancodes:

KEY_A ... KEY_Z,
KEY_0 ... KEY_9,
KEY_0_PAD ... KEY_9_PAD,
KEY_F1 ... KEY_F12,

KEY_ESC, KEY_TILDE, KEY_MINUS, KEY_EQUALS,
KEY_BACKSPACE, KEY_TAB, KEY_OPENBRACE, KEY_CLOSEBRACE,
KEY_ENTER, KEY_COLON, KEY_QUOTE, KEY_BACKSLASH,
KEY_BACKSLASH2, KEY_COMMA, KEY_STOP, KEY_SLASH,
KEY_SPACE,

KEY_INSERT, KEY_DEL, KEY_HOME, KEY_END, KEY_PGUP,
KEY_PGDN, KEY_LEFT, KEY_RIGHT, KEY_UP, KEY_DOWN,

KEY_SLASH_PAD, KEY_ASTERISK, KEY_MINUS_PAD,
KEY_PLUS_PAD, KEY_DEL_PAD, KEY_ENTER_PAD,

KEY_PRTSCR, KEY_PAUSE,

KEY_ABNT_C1, KEY_YEN, KEY_KANA, KEY_CONVERT, KEY_NOCONVERT,
KEY_AT, KEY_CIRCUMFLEX, KEY_COLON2, KEY_KANJI,

KEY_LSHIFT, KEY_RSHIFT,
KEY_LCONTROL, KEY_RCONTROL,
KEY_ALT, KEY_ALTGR,
KEY_LWIN, KEY_RWIN, KEY_MENU,
KEY_SCRLOCK, KEY_NUMLOCK, KEY_CAPSLOCK

KEY_EQUALS_PAD, KEY_BACKQUOTE, KEY_SEMICOLON, KEY_COMMAND

and finally, bestie, you might clock an odd little behaviour with the KEY_PAUSE key. 🫠 here's the tea: that key only fires off an interrupt when it's pressed β€” not when it's released. so it's giving half the story, fr fr. for this reason, Allegro pretends the pause key is a state key, which is literally the only way to make it usable. iconic workaround honestly. πŸ’…


πŸ‘‘ SEE ALSO πŸ‘‘


πŸŽ€ that's the whole page, bestie. go forth and read your keyboard like the icon you are. πŸŽ€

okay bye, this was the Allegro manual, version 4.4.3, for key(3alleg4). mwah. πŸ’œβœ¨πŸ¦‹