readkey(3)

πŸ“¦ version 4.4.3.1, in fedora 43, fedora 44

πŸ’– readkey(3) β€” keyboard buffer bestie πŸ’–

okay so you're in the Allegro manual, page readkey(3), and we're about to learn how to snag the next key from the keyboard buffer. no cap, it's giving very demure, very mindful. let's go 🫢


✨ NAME ✨

readkey β€” hands you the next character from the keyboard buffer. Allegro game programming library. πŸ’…


🌸 SYNOPSIS 🌸

#include <allegro.h>

int readkey();

πŸ’… DESCRIPTION πŸ’…

So like, readkey returns the next character from the keyboard buffer, in ASCII format. If the buffer is empty, it literally waits until a key is pressed. (Iconic behavior, honestly.) You can check if there are queued keypresses with keypressed().

The low byte of the return value contains the ASCII code of the key, and the high byte is the scancode. The scancode stays the same no matter what the shift, ctrl, and alt keys are doing, while the ASCII code gets affected by shift and ctrl in the normal way β€” shift changes case, and ctrl+letter gives you the position of that letter in the alphabet, eg. ctrl+A = 1, ctrl+B = 2, etc. If you press alt+key, you only get the scancode, and the low byte is a zero ASCII code. For example:

          int val;
          ...
          val = readkey();
          if ((val & 0xff) == 'd')     /* by ASCII code */
             allegro_message("You pressed 'd'\n");

          if ((val >> 8) == KEY_SPACE) /* by scancode */
             allegro_message("You pressed Space\n");

          if ((val & 0xff) == 3)       /* ctrl+letter */
             allegro_message("You pressed Control+C\n");

          if (val == (KEY_X << 8))     /* alt+letter */
             allegro_message("You pressed Alt+X\n");
⚠️ bestie, listen: this function cannot return character values greater than 255. If you're trying to read Unicode input, you gotta use ureadkey() instead. no exceptions. πŸ’€

πŸ¦‹ SEE ALSO πŸ¦‹


mwah πŸ’‹ that's the tea. this page is from Allegro, version 4.4.3, and it's readkey(3). stay slay, bestie βœ¨πŸ’–

πŸ“¦ where this page comes from