KEYBOARD_LOWLEVEL_CALLBACK(3alleg4)

πŸ’– NAME πŸ’–

keyboard_lowlevel_callback β€” bestie, this is your user specified low level keyboard event handler. It's part of the Allegro game programming library, and it is genuinely the rawest, most backstage-pass way to hear about keyboard stuff. πŸ‘‘


✨ SYNOPSIS ✨

#include <allegro.h>

extern void (*keyboard_lowlevel_callback)(int scancode);

πŸŽ€ DESCRIPTION πŸŽ€

Okay so picture this: if you set this function, the keyboard handler calls it in response to every single keyboard event. Not just presses β€” presses including keyboard repeat rate, and releases too. Every event. It's giving main character energy in the interrupt handler. πŸ’…

What gets passed in is a raw keyboard scancode byte. And bestie, scancodes are 7 bits long, so listen carefully:

  • the top bit (that's the 8th bit) is clear if the key has been pressed ✨
  • the top bit is set if the key has been released πŸ’€
🚨 Real talk: this routine executes in an interrupt context, so it must be in locked memory. No cap. Lock it or it's over.

Example, and yes this is the whole vibe:

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 ALSO 🌈


And that's the whole page, bestie!! 🫢 This was the Allegro manual, version 4.4.3, and honestly she ate. Go lock your memory and slay your keyboard events, ily, bye!! πŸŽ€βœ¨πŸ’–