1. package Allegro.Keyboard is 
  2.  
  3.     -- Allegro 4.2.2 - Keyboard routines 
  4.     -- This package is complete 
  5.  
  6.     type A_Keyboard_Hook is 
  7.         access procedure; 
  8.  
  9.     type A_Keyboard_Callback is 
  10.         access function( key : Integer ) return Integer; 
  11.  
  12.     type A_Keyboard_Ucallback is 
  13.         access function( key: Integer; scancode : A_Integer ) return Integer; 
  14.  
  15.     type A_Keyboard_Lowlevel_Callback is 
  16.         access procedure( scancode : Integer ); 
  17.  
  18.     ---------------------------------------------------------------------------- 
  19.  
  20.     KB_SHIFT_FLAG    : constant Unsigned_32; 
  21.     KB_CTRL_FLAG     : constant Unsigned_32; 
  22.     KB_ALT_FLAG      : constant Unsigned_32; 
  23.     KB_LWIN_FLAG     : constant Unsigned_32; 
  24.     KB_RWIN_FLAG     : constant Unsigned_32; 
  25.     KB_MENU_FLAG     : constant Unsigned_32; 
  26.     KB_COMMAND_FLAG  : constant Unsigned_32; 
  27.     KB_SCROLOCK_FLAG : constant Unsigned_32; 
  28.     KB_NUMLOCK_FLAG  : constant Unsigned_32; 
  29.     KB_CAPSLOCK_FLAG : constant Unsigned_32; 
  30.     KB_INALTSEQ_FLAG : constant Unsigned_32; 
  31.     KB_ACCENT1_FLAG  : constant Unsigned_32; 
  32.     KB_ACCENT2_FLAG  : constant Unsigned_32; 
  33.     KB_ACCENT3_FLAG  : constant Unsigned_32; 
  34.     KB_ACCENT4_FLAG  : constant Unsigned_32; 
  35.  
  36.     KEY_A          : constant Integer := 1; 
  37.     KEY_B          : constant Integer := 2; 
  38.     KEY_C          : constant Integer := 3; 
  39.     KEY_D          : constant Integer := 4; 
  40.     KEY_E          : constant Integer := 5; 
  41.     KEY_F          : constant Integer := 6; 
  42.     KEY_G          : constant Integer := 7; 
  43.     KEY_H          : constant Integer := 8; 
  44.     KEY_I          : constant Integer := 9; 
  45.     KEY_J          : constant Integer := 10; 
  46.     KEY_K          : constant Integer := 11; 
  47.     KEY_L          : constant Integer := 12; 
  48.     KEY_M          : constant Integer := 13; 
  49.     KEY_N          : constant Integer := 14; 
  50.     KEY_O          : constant Integer := 15; 
  51.     KEY_P          : constant Integer := 16; 
  52.     KEY_Q          : constant Integer := 17; 
  53.     KEY_R          : constant Integer := 18; 
  54.     KEY_S          : constant Integer := 19; 
  55.     KEY_T          : constant Integer := 20; 
  56.     KEY_U          : constant Integer := 21; 
  57.     KEY_V          : constant Integer := 22; 
  58.     KEY_W          : constant Integer := 23; 
  59.     KEY_X          : constant Integer := 24; 
  60.     KEY_Y          : constant Integer := 25; 
  61.     KEY_Z          : constant Integer := 26; 
  62.     KEY_0          : constant Integer := 27; 
  63.     KEY_1          : constant Integer := 28; 
  64.     KEY_2          : constant Integer := 29; 
  65.     KEY_3          : constant Integer := 30; 
  66.     KEY_4          : constant Integer := 31; 
  67.     KEY_5          : constant Integer := 32; 
  68.     KEY_6          : constant Integer := 33; 
  69.     KEY_7          : constant Integer := 34; 
  70.     KEY_8          : constant Integer := 35; 
  71.     KEY_9          : constant Integer := 36; 
  72.     KEY_0_PAD      : constant Integer := 37; 
  73.     KEY_1_PAD      : constant Integer := 38; 
  74.     KEY_2_PAD      : constant Integer := 39; 
  75.     KEY_3_PAD      : constant Integer := 40; 
  76.     KEY_4_PAD      : constant Integer := 41; 
  77.     KEY_5_PAD      : constant Integer := 42; 
  78.     KEY_6_PAD      : constant Integer := 43; 
  79.     KEY_7_PAD      : constant Integer := 44; 
  80.     KEY_8_PAD      : constant Integer := 45; 
  81.     KEY_9_PAD      : constant Integer := 46; 
  82.     KEY_F1         : constant Integer := 47; 
  83.     KEY_F2         : constant Integer := 48; 
  84.     KEY_F3         : constant Integer := 49; 
  85.     KEY_F4         : constant Integer := 50; 
  86.     KEY_F5         : constant Integer := 51; 
  87.     KEY_F6         : constant Integer := 52; 
  88.     KEY_F7         : constant Integer := 53; 
  89.     KEY_F8         : constant Integer := 54; 
  90.     KEY_F9         : constant Integer := 55; 
  91.     KEY_F10        : constant Integer := 56; 
  92.     KEY_F11        : constant Integer := 57; 
  93.     KEY_F12        : constant Integer := 58; 
  94.     KEY_ESC        : constant Integer := 59; 
  95.     KEY_TILDE      : constant Integer := 60; 
  96.     KEY_MINUS      : constant Integer := 61; 
  97.     KEY_EQUALS     : constant Integer := 62; 
  98.     KEY_BACKSPACE  : constant Integer := 63; 
  99.     KEY_TAB        : constant Integer := 64; 
  100.     KEY_OPENBRACE  : constant Integer := 65; 
  101.     KEY_CLOSEBRACE : constant Integer := 66; 
  102.     KEY_ENTER      : constant Integer := 67; 
  103.     KEY_COLON      : constant Integer := 68; 
  104.     KEY_QUOTE      : constant Integer := 69; 
  105.     KEY_BACKSLASH  : constant Integer := 70; 
  106.     KEY_BACKSLASH2 : constant Integer := 71; 
  107.     KEY_COMMA      : constant Integer := 72; 
  108.     KEY_STOP       : constant Integer := 73; 
  109.     KEY_SLASH      : constant Integer := 74; 
  110.     KEY_SPACE      : constant Integer := 75; 
  111.     KEY_INSERT     : constant Integer := 76; 
  112.     KEY_DEL        : constant Integer := 77; 
  113.     KEY_HOME       : constant Integer := 78; 
  114.     KEY_END        : constant Integer := 79; 
  115.     KEY_PGUP       : constant Integer := 80; 
  116.     KEY_PGDN       : constant Integer := 81; 
  117.     KEY_LEFT       : constant Integer := 82; 
  118.     KEY_RIGHT      : constant Integer := 83; 
  119.     KEY_UP         : constant Integer := 84; 
  120.     KEY_DOWN       : constant Integer := 85; 
  121.     KEY_SLASH_PAD  : constant Integer := 86; 
  122.     KEY_ASTERISK   : constant Integer := 87; 
  123.     KEY_MINUS_PAD  : constant Integer := 88; 
  124.     KEY_PLUS_PAD   : constant Integer := 89; 
  125.     KEY_DEL_PAD    : constant Integer := 90; 
  126.     KEY_ENTER_PAD  : constant Integer := 91; 
  127.     KEY_PRTSCR     : constant Integer := 92; 
  128.     KEY_PAUSE      : constant Integer := 93; 
  129.     KEY_ABNT_C1    : constant Integer := 94; 
  130.     KEY_YEN        : constant Integer := 95; 
  131.     KEY_KANA       : constant Integer := 96; 
  132.     KEY_CONVERT    : constant Integer := 97; 
  133.     KEY_NOCONVERT  : constant Integer := 98; 
  134.     KEY_AT         : constant Integer := 99; 
  135.     KEY_CIRCUMFLEX : constant Integer := 100; 
  136.     KEY_COLON2     : constant Integer := 101; 
  137.     KEY_KANJI      : constant Integer := 102; 
  138.     KEY_EQUALS_PAD : constant Integer := 103;  -- MacOS X 
  139.     KEY_BACKQUOTE  : constant Integer := 104;  -- MacOS X 
  140.     KEY_SEMICOLON  : constant Integer := 105;  -- MacOS X 
  141.     KEY_COMMAND    : constant Integer := 106;  -- MacOS X 
  142.     KEY_UNKNOWN1   : constant Integer := 107; 
  143.     KEY_UNKNOWN2   : constant Integer := 108; 
  144.     KEY_UNKNOWN3   : constant Integer := 109; 
  145.     KEY_UNKNOWN4   : constant Integer := 110; 
  146.     KEY_UNKNOWN5   : constant Integer := 111; 
  147.     KEY_UNKNOWN6   : constant Integer := 112; 
  148.     KEY_UNKNOWN7   : constant Integer := 113; 
  149.     KEY_UNKNOWN8   : constant Integer := 114; 
  150.  
  151.     KEY_MODIFIERS  : constant Integer := 115; 
  152.  
  153.     KEY_LSHIFT     : constant Integer := 115; 
  154.     KEY_RSHIFT     : constant Integer := 116; 
  155.     KEY_LCONTROL   : constant Integer := 117; 
  156.     KEY_RCONTROL   : constant Integer := 118; 
  157.     KEY_ALT        : constant Integer := 119; 
  158.     KEY_ALTGR      : constant Integer := 120; 
  159.     KEY_LWIN       : constant Integer := 121; 
  160.     KEY_RWIN       : constant Integer := 122; 
  161.     KEY_MENU       : constant Integer := 123; 
  162.     KEY_SCRLOCK    : constant Integer := 124; 
  163.     KEY_NUMLOCK    : constant Integer := 125; 
  164.     KEY_CAPSLOCK   : constant Integer := 126; 
  165.  
  166.     KEY_MAX        : constant Integer; 
  167.  
  168.     ---------------------------------------------------------------------------- 
  169.  
  170.     procedure Clear_Keybuf; 
  171.  
  172.     function Get_Key_Status( keycode : Integer ) return Integer; 
  173.     pragma Precondition( keycode > 0 and then keycode < KEY_MAX ); 
  174.  
  175.     function Get_Key_Shifts return Unsigned_32; 
  176.  
  177.     function Install_Keyboard return Integer; 
  178.  
  179.     procedure Install_Keyboard_Hooks( keypressed, readkey : A_Keyboard_Hook ); 
  180.  
  181.     function Key_Pressed return Integer; 
  182.  
  183.     function Keyboard_Needs_Poll return Integer; 
  184.  
  185.     function Poll_Keyboard return Integer; 
  186.  
  187.     function Read_Key return Integer; 
  188.  
  189.     procedure Remove_Keyboard; 
  190.  
  191.     function Scancode_To_Ascii( scancode : Integer ) return Integer; 
  192.     pragma Precondition( scancode > 0 and then scancode < KEY_MAX ); 
  193.  
  194.     function Scancode_To_Char( scancode : Integer; 
  195.                                shifted  : Boolean ) return Character; 
  196.     pragma Precondition( scancode > 0 and then scancode < KEY_MAX ); 
  197.  
  198.     function Scancode_To_Name( scancode : Integer ) return String; 
  199.     pragma Precondition( scancode > 0 and then scancode < KEY_MAX ); 
  200.  
  201.     procedure Set_Key_Led_Flag( flag : Integer ); 
  202.  
  203.     procedure Set_Keyboard_Callback( cb : A_Keyboard_Callback ); 
  204.  
  205.     procedure Set_Keyboard_Lowlevel_Callback( cb : A_Keyboard_Lowlevel_Callback ); 
  206.  
  207.     procedure Set_Keyboard_Rate( dlay, repeat : Integer ); 
  208.  
  209.     procedure Set_Keyboard_Ucallback( cb : A_Keyboard_Ucallback ); 
  210.  
  211.     procedure Set_Leds( leds : Integer ); 
  212.  
  213.     procedure Set_Three_Finger_Flag( flag : Integer ); 
  214.  
  215.     procedure Simulate_Keypress( key : Integer ); 
  216.  
  217.     procedure Simulate_Ukeypress( key, scancode : Integer ); 
  218.     pragma Precondition( scancode >= 0 and then scancode < KEY_MAX ); 
  219.  
  220.     function Uread_Key( scancode : A_Integer ) return Integer; 
  221.  
  222. private 
  223.  
  224.     KB_SHIFT_FLAG    : constant Unsigned_32 := 16#00000001#; 
  225.     KB_CTRL_FLAG     : constant Unsigned_32 := 16#00000002#; 
  226.     KB_ALT_FLAG      : constant Unsigned_32 := 16#00000004#; 
  227.     KB_LWIN_FLAG     : constant Unsigned_32 := 16#00000008#; 
  228.     KB_RWIN_FLAG     : constant Unsigned_32 := 16#00000010#; 
  229.     KB_MENU_FLAG     : constant Unsigned_32 := 16#00000020#; 
  230.     KB_COMMAND_FLAG  : constant Unsigned_32 := 16#00000040#; 
  231.     KB_SCROLOCK_FLAG : constant Unsigned_32 := 16#00000100#; 
  232.     KB_NUMLOCK_FLAG  : constant Unsigned_32 := 16#00000200#; 
  233.     KB_CAPSLOCK_FLAG : constant Unsigned_32 := 16#00000400#; 
  234.     KB_INALTSEQ_FLAG : constant Unsigned_32 := 16#00000800#; 
  235.     KB_ACCENT1_FLAG  : constant Unsigned_32 := 16#00001000#; 
  236.     KB_ACCENT2_FLAG  : constant Unsigned_32 := 16#00002000#; 
  237.     KB_ACCENT3_FLAG  : constant Unsigned_32 := 16#00004000#; 
  238.     KB_ACCENT4_FLAG  : constant Unsigned_32 := 16#00008000#; 
  239.  
  240.     KEY_MAX          : constant Integer := 127; 
  241.  
  242.     ---------------------------------------------------------------------------- 
  243.  
  244.     type Key_Character_Array is array (1..KEY_MAX) of Character; 
  245.  
  246.     scancode_to_char_lo : constant Key_Character_Array := 
  247.         Key_Character_Array'( 
  248.             KEY_A          => 'a', 
  249.             KEY_B          => 'b', 
  250.             KEY_C          => 'c', 
  251.             KEY_D          => 'd', 
  252.             KEY_E          => 'e', 
  253.             KEY_F          => 'f', 
  254.             KEY_G          => 'g', 
  255.             KEY_H          => 'h', 
  256.             KEY_I          => 'i', 
  257.             KEY_J          => 'j', 
  258.             KEY_K          => 'k', 
  259.             KEY_L          => 'l', 
  260.             KEY_M          => 'm', 
  261.             KEY_N          => 'n', 
  262.             KEY_O          => 'o', 
  263.             KEY_P          => 'p', 
  264.             KEY_Q          => 'q', 
  265.             KEY_R          => 'r', 
  266.             KEY_S          => 's', 
  267.             KEY_T          => 't', 
  268.             KEY_U          => 'u', 
  269.             KEY_V          => 'v', 
  270.             KEY_W          => 'w', 
  271.             KEY_X          => 'x', 
  272.             KEY_Y          => 'y', 
  273.             KEY_Z          => 'z', 
  274.             KEY_0          => '0', 
  275.             KEY_1          => '1', 
  276.             KEY_2          => '2', 
  277.             KEY_3          => '3', 
  278.             KEY_4          => '4', 
  279.             KEY_5          => '5', 
  280.             KEY_6          => '6', 
  281.             KEY_7          => '7', 
  282.             KEY_8          => '8', 
  283.             KEY_9          => '9', 
  284.             KEY_0_PAD      => '0', 
  285.             KEY_1_PAD      => '1', 
  286.             KEY_2_PAD      => '2', 
  287.             KEY_3_PAD      => '3', 
  288.             KEY_4_PAD      => '4', 
  289.             KEY_5_PAD      => '5', 
  290.             KEY_6_PAD      => '6', 
  291.             KEY_7_PAD      => '7', 
  292.             KEY_8_PAD      => '8', 
  293.             KEY_9_PAD      => '9', 
  294.             KEY_TILDE      => '`', 
  295.             KEY_MINUS      => '-', 
  296.             KEY_EQUALS     => '=', 
  297.             KEY_OPENBRACE  => '[', 
  298.             KEY_CLOSEBRACE => ']', 
  299.             KEY_COLON      => ';', 
  300.             KEY_QUOTE      => ''', 
  301.             KEY_BACKSLASH  => '\', 
  302.             KEY_BACKSLASH2 => '\', 
  303.             KEY_COMMA      => ',', 
  304.             KEY_STOP       => '.', 
  305.             KEY_SLASH      => '/', 
  306.             KEY_SPACE      => ' ', 
  307.             KEY_SLASH_PAD  => '/', 
  308.             KEY_ASTERISK   => '*', 
  309.             KEY_MINUS_PAD  => '-', 
  310.             KEY_PLUS_PAD   => '+', 
  311.             KEY_DEL_PAD    => '.', 
  312.  
  313.             others => Character'Val( 0 ) 
  314.         ); 
  315.  
  316.     scancode_to_char_hi : constant Key_Character_Array := 
  317.         Key_Character_Array'( 
  318.             KEY_A          => 'A', 
  319.             KEY_B          => 'B', 
  320.             KEY_C          => 'C', 
  321.             KEY_D          => 'D', 
  322.             KEY_E          => 'E', 
  323.             KEY_F          => 'F', 
  324.             KEY_G          => 'G', 
  325.             KEY_H          => 'H', 
  326.             KEY_I          => 'I', 
  327.             KEY_J          => 'J', 
  328.             KEY_K          => 'K', 
  329.             KEY_L          => 'L', 
  330.             KEY_M          => 'M', 
  331.             KEY_N          => 'N', 
  332.             KEY_O          => 'O', 
  333.             KEY_P          => 'P', 
  334.             KEY_Q          => 'Q', 
  335.             KEY_R          => 'R', 
  336.             KEY_S          => 'S', 
  337.             KEY_T          => 'T', 
  338.             KEY_U          => 'U', 
  339.             KEY_V          => 'V', 
  340.             KEY_W          => 'W', 
  341.             KEY_X          => 'X', 
  342.             KEY_Y          => 'Y', 
  343.             KEY_Z          => 'Z', 
  344.             KEY_0          => ')', 
  345.             KEY_1          => '!', 
  346.             KEY_2          => '@', 
  347.             KEY_3          => '#', 
  348.             KEY_4          => '$', 
  349.             KEY_5          => '%', 
  350.             KEY_6          => '^', 
  351.             KEY_7          => '&', 
  352.             KEY_8          => '*', 
  353.             KEY_9          => '(', 
  354.             KEY_0_PAD      => '0', 
  355.             KEY_1_PAD      => '1', 
  356.             KEY_2_PAD      => '2', 
  357.             KEY_3_PAD      => '3', 
  358.             KEY_4_PAD      => '4', 
  359.             KEY_5_PAD      => '5', 
  360.             KEY_6_PAD      => '6', 
  361.             KEY_7_PAD      => '7', 
  362.             KEY_8_PAD      => '8', 
  363.             KEY_9_PAD      => '9', 
  364.             KEY_TILDE      => '~', 
  365.             KEY_MINUS      => '_', 
  366.             KEY_EQUALS     => '+', 
  367.             KEY_OPENBRACE  => '{', 
  368.             KEY_CLOSEBRACE => '}', 
  369.             KEY_COLON      => ':', 
  370.             KEY_QUOTE      => '"', 
  371.             KEY_BACKSLASH  => '|', 
  372.             KEY_BACKSLASH2 => '|', 
  373.             KEY_COMMA      => '<', 
  374.             KEY_STOP       => '>', 
  375.             KEY_SLASH      => '?', 
  376.             KEY_SPACE      => ' ', 
  377.             KEY_SLASH_PAD  => '/', 
  378.             KEY_ASTERISK   => '*', 
  379.             KEY_MINUS_PAD  => '-', 
  380.             KEY_PLUS_PAD   => '+', 
  381.             KEY_DEL_PAD    => '.', 
  382.  
  383.             others         => Character'Val( 0 ) 
  384.         ); 
  385.  
  386.     ---------------------------------------------------------------------------- 
  387.  
  388.     pragma Convention( C, A_Keyboard_Callback ); 
  389.     pragma Convention( C, A_Keyboard_Hook ); 
  390.     pragma Convention( C, A_Keyboard_Lowlevel_Callback ); 
  391.     pragma Convention( C, A_Keyboard_Ucallback ); 
  392.  
  393.     pragma Import( C, Clear_Keybuf, "clear_keybuf" ); 
  394.     pragma Import( C, Get_Key_Shifts, "get_key_shifts" ); 
  395.     pragma Import( C, Get_Key_Status, "get_key_status" ); 
  396.     pragma Import( C, Install_Keyboard, "install_keyboard" ); 
  397.     pragma Import( C, Install_Keyboard_Hooks, "install_keyboard_hooks" ); 
  398.     pragma Import( C, Keyboard_Needs_Poll, "keyboard_needs_poll" ); 
  399.     pragma Import( C, Key_Pressed, "keypressed" ); 
  400.     pragma Import( C, Poll_Keyboard, "poll_keyboard" ); 
  401.     pragma Import( C, Read_Key, "readkey" ); 
  402.     pragma Import( C, Remove_Keyboard, "remove_keyboard" ); 
  403.     pragma Import( C, Scancode_To_Ascii, "scancode_to_ascii" ); 
  404.     pragma Import( C, Set_Key_Led_Flag, "set_key_led_flag" ); 
  405.     pragma Import( C, Set_Keyboard_Callback, "set_keyboard_callback" ); 
  406.     pragma Import( C, Set_Keyboard_Lowlevel_Callback, "set_keyboard_lowlevel_callback" ); 
  407.     pragma Import( C, Set_Keyboard_Rate, "set_keyboard_rate" ); 
  408.     pragma Import( C, Set_Keyboard_Ucallback, "set_keyboard_ucallback" ); 
  409.     pragma Import( C, Set_Leds, "set_leds" ); 
  410.     pragma Import( C, Set_Three_Finger_Flag, "set_three_finger_flag" ); 
  411.     pragma Import( C, Simulate_Keypress, "simulate_keypress" ); 
  412.     pragma Import( C, Simulate_Ukeypress, "simulate_ukeypress" ); 
  413.     pragma Import( C, Uread_Key, "ureadkey" ); 
  414.  
  415. end Allegro.Keyboard;