Code Search for Developers
 
 
  

exkeys.c from Allegro game programming library at Krugle


Show exkeys.c syntax highlighted

/*
 *    Example program for the Allegro library, by Shawn Hargreaves.
 *
 *    This program demonstrates how to access the keyboard.
 */


#include <stdio.h>
#include <string.h>

#include "allegro.h"



char *key_names[] =
{
   "(none)",         "AL_KEY_A",          "AL_KEY_B",          "AL_KEY_C",
   "AL_KEY_D",          "AL_KEY_E",          "AL_KEY_F",          "AL_KEY_G",
   "AL_KEY_H",          "AL_KEY_I",          "AL_KEY_J",          "AL_KEY_K",
   "AL_KEY_L",          "AL_KEY_M",          "AL_KEY_N",          "AL_KEY_O",
   "AL_KEY_P",          "AL_KEY_Q",          "AL_KEY_R",          "AL_KEY_S",
   "AL_KEY_T",          "AL_KEY_U",          "AL_KEY_V",          "AL_KEY_W",
   "AL_KEY_X",          "AL_KEY_Y",          "AL_KEY_Z",          "AL_KEY_0",
   "AL_KEY_1",          "AL_KEY_2",          "AL_KEY_3",          "AL_KEY_4",
   "AL_KEY_5",          "AL_KEY_6",          "AL_KEY_7",          "AL_KEY_8",
   "AL_KEY_9",          "AL_KEY_0_PAD",      "AL_KEY_1_PAD",      "AL_KEY_2_PAD",
   "AL_KEY_3_PAD",      "AL_KEY_4_PAD",      "AL_KEY_5_PAD",      "AL_KEY_6_PAD",
   "AL_KEY_7_PAD",      "AL_KEY_8_PAD",      "AL_KEY_9_PAD",      "AL_KEY_F1",
   "AL_KEY_F2",         "AL_KEY_F3",         "AL_KEY_F4",         "AL_KEY_F5",
   "AL_KEY_F6",         "AL_KEY_F7",         "AL_KEY_F8",         "AL_KEY_F9",
   "AL_KEY_F10",        "AL_KEY_F11",        "AL_KEY_F12",        "AL_KEY_ESC",
   "AL_KEY_TILDE",      "AL_KEY_MINUS",      "AL_KEY_EQUALS",     "AL_KEY_BACKSPACE",
   "AL_KEY_TAB",        "AL_KEY_OPENBRACE",  "AL_KEY_CLOSEBRACE", "AL_KEY_ENTER",
   "AL_KEY_COLON",      "AL_KEY_QUOTE",      "AL_KEY_BACKSLASH",  "AL_KEY_BACKSLASH2",
   "AL_KEY_COMMA",      "AL_KEY_STOP",       "AL_KEY_SLASH",      "AL_KEY_SPACE",
   "AL_KEY_INSERT",     "AL_KEY_DEL",        "AL_KEY_HOME",       "AL_KEY_END",
   "AL_KEY_PGUP",       "AL_KEY_PGDN",       "AL_KEY_LEFT",       "AL_KEY_RIGHT",
   "AL_KEY_UP",         "AL_KEY_DOWN",       "AL_KEY_SLASH_PAD",  "AL_KEY_ASTERISK",
   "AL_KEY_MINUS_PAD",  "AL_KEY_PLUS_PAD",   "AL_KEY_DEL_PAD",    "AL_KEY_ENTER_PAD",
   "AL_KEY_PRTSCR",     "AL_KEY_PAUSE",      "AL_KEY_ABNT_C1",    "AL_KEY_YEN",
   "AL_KEY_KANA",       "AL_KEY_CONVERT",    "AL_KEY_NOCONVERT",  "AL_KEY_AT",
   "AL_KEY_CIRCUMFLEX", "AL_KEY_COLON2",     "AL_KEY_KANJI",
   "AL_KEY_LSHIFT",     "AL_KEY_RSHIFT",     "AL_KEY_LCONTROL",   "AL_KEY_RCONTROL",
   "AL_KEY_ALT",        "AL_KEY_ALTGR",      "AL_KEY_LWIN",       "AL_KEY_RWIN",
   "AL_KEY_MENU",       "AL_KEY_SCRLOCK",    "AL_KEY_NUMLOCK",    "AL_KEY_CAPSLOCK",
   "AL_KEY_MAX"
};



/* helper function for making more room on the al_screen */
void scroll(void)
{
   al_blit(al_screen, al_screen, 0, 32, 0, 24, AL_SCREEN_W, AL_SCREEN_H-32);
   al_draw_rect_fill(al_screen, 0, AL_SCREEN_H-16, AL_SCREEN_W, AL_SCREEN_H, al_make_color(255, 255, 255));
}



int main(void)
{
   char buf[128];
   int k;

   allegro_init();
   al_install_keyboard(); 
   if (al_set_gfx_mode(AL_GFX_SAFE, 320, 200, 0, 0) != 0) {
      al_set_gfx_mode(AL_GFX_NONE, 0, 0, 0, 0);
      al_show_message("Unable to set any graphic mode\n%s\n", al_error);
      return 1;
   }
   al_set_palette(al_desktop_palette);
   al_text_mode(al_make_color(255, 255, 255));

   al_acquire_screen();
   al_clear_to_color(al_screen, al_make_color(255, 255, 255));
   al_printf_text_centre(al_screen, al_font_8x8, AL_SCREEN_W/2, 8, al_make_color(0, 0, 0),
		     "Driver: %s", keyboard_driver->name);

   /* keyboard input can be accessed with the al_read_key() function */
   al_printf_text(al_screen, al_font_8x8, 8, AL_SCREEN_H-16, al_make_color(0, 0, 0),
	      "Press some keys (ESC to finish)");
   scroll();

   do {
      al_release_screen();
      k = al_read_key();
      al_acquire_screen();
      scroll();
      al_printf_text(al_screen, al_font_8x8, 8, AL_SCREEN_H-16, al_make_color(0, 0, 0),
		 "al_read_key() returned %-6d (0x%04X)", k, k);
   } while ((k & 0xFF) != 27);

   /* the ASCII code is in the low byte of the return value */
   scroll(); scroll(); scroll();
   al_printf_text(al_screen, al_font_8x8, 8, AL_SCREEN_H-16, al_make_color(0, 0, 0),
	      "Press some more keys (ESC to finish)");
   scroll();

   do {
      al_release_screen();
      k = al_read_key();
      al_acquire_screen();
      scroll();
      al_printf_text(al_screen, al_font_8x8, 8, AL_SCREEN_H-16, al_make_color(0, 0, 0),
		 "ASCII code is %d", k&0xFF);
   } while ((k&0xFF) != 27);

   /* the hardware scancode is in the high byte of the return value */
   scroll(); scroll(); scroll();
   al_printf_text(al_screen, al_font_8x8, 8, AL_SCREEN_H-16, al_make_color(0, 0, 0),
	      "Press some more keys (ESC to finish)");
   scroll();

   do {
      al_release_screen();
      k = al_read_key();
      al_acquire_screen();
      scroll();
      al_printf_text(al_screen, al_font_8x8, 8, AL_SCREEN_H-16, al_make_color(0, 0, 0),
		 "Scancode is %d (%s)", k>>8, key_names[k>>8]);
   } while ((k&0xFF) != 27);

   /* al_key qualifiers are stored in the al_key_shifts variable. Note that this
    * version of the code uses al_uread_key() instead of al_read_key(): that is
    * necessary if you want to access Unicode characters from outside
    * the normal ASCII range, for example to support Russian or Chinese.
    */
   scroll(); scroll(); scroll();
   al_printf_text(al_screen, al_font_8x8, 8, AL_SCREEN_H-16, al_make_color(0, 0, 0),
	      "Press some more keys (ESC to finish)");
   scroll();

   do {
      al_release_screen();
      k = al_uread_key(NULL);
      al_acquire_screen();
      buf[0] = 0;
      if (al_key_shifts & KB_SHIFT_FLAG) strcat(buf, "shift ");
      if (al_key_shifts & KB_CTRL_FLAG)  strcat(buf, "ctrl ");
      if (al_key_shifts & KB_ALT_FLAG)   strcat(buf, "alt ");
      if (al_key_shifts & KB_LWIN_FLAG)  strcat(buf, "lwin ");
      if (al_key_shifts & KB_RWIN_FLAG)  strcat(buf, "rwin ");
      if (al_key_shifts & KB_MENU_FLAG)  strcat(buf, "menu ");
      al_usprintf(buf+strlen(buf), "'%c' [0x%02x]", k ? k : ' ', k);
      if (al_key_shifts & KB_CAPSLOCK_FLAG) strcat(buf, " caps");
      if (al_key_shifts & KB_NUMLOCK_FLAG)  strcat(buf, " num");
      if (al_key_shifts & KB_SCROLOCK_FLAG) strcat(buf, " scrl");
      scroll();
      al_printf_text(al_screen, al_font_8x8, 8, AL_SCREEN_H-16, al_make_color(0, 0, 0), buf);
   } while (k != 27);

   /* various scancodes are defined in allegro.h as AL_KEY_* constants */
   scroll(); scroll(); scroll();
   al_printf_text(al_screen, al_font_8x8, 8, AL_SCREEN_H-16, al_make_color(0, 0, 0), "Press F6");
   scroll();

   al_release_screen();
   k = al_read_key();
   al_acquire_screen();

   while ((k>>8) != AL_KEY_F6) {
      scroll();
      al_printf_text(al_screen, al_font_8x8, 8, AL_SCREEN_H-16, al_make_color(0, 0, 0),
		 "Wrong al_key, stupid! I said press F6");
      al_release_screen();
      k = al_read_key();
      al_acquire_screen();
   }

   /* for detecting multiple simultaneous keypresses, use the al_key[] array */
   scroll(); scroll(); scroll();
   al_printf_text(al_screen, al_font_8x8, 8, AL_SCREEN_H-16, al_make_color(0, 0, 0),
	      "Press a combination of numbers");
   scroll(); scroll();

   al_release_screen();

   do {
      if (al_key[AL_KEY_0]) buf[0] = '0'; else buf[0] = ' ';
      if (al_key[AL_KEY_1]) buf[1] = '1'; else buf[1] = ' ';
      if (al_key[AL_KEY_2]) buf[2] = '2'; else buf[2] = ' ';
      if (al_key[AL_KEY_3]) buf[3] = '3'; else buf[3] = ' ';
      if (al_key[AL_KEY_4]) buf[4] = '4'; else buf[4] = ' ';
      if (al_key[AL_KEY_5]) buf[5] = '5'; else buf[5] = ' ';
      if (al_key[AL_KEY_6]) buf[6] = '6'; else buf[6] = ' ';
      if (al_key[AL_KEY_7]) buf[7] = '7'; else buf[7] = ' ';
      if (al_key[AL_KEY_8]) buf[8] = '8'; else buf[8] = ' ';
      if (al_key[AL_KEY_9]) buf[9] = '9'; else buf[9] = ' ';
      buf[10] = 0;
      al_printf_text(al_screen, al_font_8x8, 8, AL_SCREEN_H-16, al_make_color(0, 0, 0), buf);
   } while (!al_key[AL_KEY_ESC]);

   al_clear_keybuf();
   return 0;
}

AL_END_OF_MAIN();




See more files for this project here

Allegro game programming library

Allegro is a cross-platform library intended for use in computer games and other types of multimedia programming.

Project homepage: http://sourceforge.net/projects/alleg
Programming language(s): Assembly,C,Shell Script
License: other

  allegro.pcx
  ex12bit.c
  ex3buf.c
  ex3d.c
  exaccel.c
  exalpha.c
  example.dat
  example.h
  examples.txt
  exbitmap.c
  exblend.c
  excamera.c
  excolmap.c
  excustom.c
  exdata.c
  exdbuf.c
  exdodgy.c
  exexedat.c
  exfixed.c
  exflame.c
  exflip.c
  exgui.c
  exhello.c
  exjoy.c
  exkeys.c
  exlights.c
  exmem.c
  exmidi.c
  exmouse.c
  expal.c
  expat.c
  exquat.c
  exrgbhsv.c
  exsample.c
  exscale.c
  exscn3d.c
  exscroll.c
  exshade.c
  exspline.c
  exsprite.c
  exstars.c
  exstream.c
  exswitch.c
  extimer.c
  extrans.c
  extruec.c
  exunicod.c
  exupdate.c
  exxfade.c
  exzbuf.c
  mysha.pcx
  planet.pcx
  running.dat
  running.h
  unifont.dat