keyboard_lowlevel_callback(3)

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

πŸ’– keyboard_lowlevel_callback πŸ’–

okay bestie, so this is a page from the Allegro manual and it's all about keyboard_lowlevel_callback, which is genuinely one of the most "i'm built different" functions in the whole library. buckle up πŸŽ€


✨ NAME ✨

keyboard_lowlevel_callback β€” the user-specified low level keyboard event handler, living inside the Allegro game programming library. she's the girl behind the curtain at every single key event 🫢


🌸 SYNOPSIS 🌸

#include <allegro.h>

extern void (*keyboard_lowlevel_callback)(int scancode);

πŸ¦‹ DESCRIPTION πŸ¦‹

so like, if you set this function, the keyboard handler is gonna call it in response to every single keyboard event β€” no cap. and when we say every, we mean every: both presses (yes, including the keyboard repeat rate, so it'll spam you the way your group chat spams you) and releases. πŸ’…

what gets passed in? a raw keyboard scancode byte. and bestie, scancodes are 7 bits long, so listen closely to the bit situation:

  • the top bit (the 8th bit) is clear if the key has been pressed
  • and it's set if the key was released
🚨 ok this part is important, do not skip it: this routine executes in an interrupt context, so it must be in locked memory. like, that's not optional, that's the vibe check you have to pass or things go bad.

here's the example, and it's giving exactly what you need:

volatile int key_down, key_up;

void keypress_watcher(int scancode)
{
   if (scancode & 0x80) {
      key_up = 1;
   } else {
      key_down = 1;
   }
} END_OF_FUNCTION(keypress_watcher)

...

   install_timer();
   LOCK_FUNCTION(silence_g_key);
   LOCK_VARIABLE(key_down);
   LOCK_VARIABLE(key_up);
   install_keyboard();
   keyboard_lowlevel_callback = keypress_watcher;
   /* Disable keyboard repeat to get typewriter effect. */
   set_keyboard_rate(0, 0);

...

   while (game_loop) {
      if (key_down) {
         key_down = 0;
         /* Play sample of typewriter key press. */
      }
      if (key_up) {
         key_up = 0;
         /* Play sample of typewriter key release. */
      }
   }

see how she's locking the variables and the function before installing? that's the whole assignment, periodt. πŸ”₯


πŸ‘‘ SEE ALSO πŸ‘‘


and that's the whole page, bestie πŸ’œ this one's from Allegro, version 4.4.3 β€” mwah, iconic, no notes πŸ’…βœ¨πŸŽ€

πŸ“¦ where this page comes from