The Allegro keyboard handler provides both buffered input and a set of flags storing the current state of each key. Note that it is not possible to correctly detect every combination of keys, due to the design of the PC keyboard. Up to two or three keys at a time will work fine, but if you press more than that the extras are likely to be ignored (exactly which combinations are possible seems to vary from one keyboard to another).
On DOS, Allegro requires the user to specify the language of the keyboard mapping because it is impossible to obtain this information from the OS, otherwise the default US keyboard mapping will be used. Allegro comes with a prepackaged `keyboard.dat' file which you can put along with your binary. If this file is present, Allegro will be able to extract the keyboard mapping information stored there. However, the end user still needs to select which keyboard mapping to use. This can be accomplished through the keyboard variable of the system section in a standard `allegro.cfg' configuration file. Read chapter "Configuration routines" for more information about this.
Note that on some platforms the keyboard won't work unless you have set a graphics mode, even if this function returns a success value before calling set_gfx_mode. This can happen in environments with graphic windowed modes, since Allegro usually reads the keyboard through the graphical window (which appears after the set_gfx_mode call). Example:
allegro_init(); install_timer(); install_keyboard(); /* We are not 100% sure we can read the keyboard yet! */ if (set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0) != 0) abort_on_error("Couldn't set graphic mode!"); /* Now we are guaranteed to be able to read the keyboard. */ readkey();
Return value: Returns zero on success, or a negative number on failure (but you may decide not to check the return value as this function is very unlikely to fail).
See also: remove_keyboard, poll_keyboard, key, keypressed, readkey, ureadkey, keyboard_callback, keyboard_ucallback, keyboard_lowlevel_callback, three_finger_flag, key_led_flag, set_leds, set_keyboard_rate, set_gfx_mode, Standard config variables.
Examples using this: Available Allegro examples.
remove_keyboard(); /* Switch to Spanish keyboard mapping. */ set_config_string("system", "keyboard", "es"); install_keyboard();
See also: install_keyboard, allegro_exit, set_config_string.
See also: install_keyboard, keypressed, readkey.
To help you test your keyboard polling code even if you are programming on a platform that doesn't require it, after the first time that you call this function Allegro will switch into polling mode, so from that point onwards you will have to call this routine in order to get any keyboard input at all, regardless of whether the current driver actually needs to be polled or not.
The keypressed(), readkey(), and ureadkey() functions call poll_keyboard() automatically, so you only need to use this function when accessing the key[] array and key_shifts variable.
Return value: Returns zero on success, or a negative number on failure (ie. no keyboard driver installed).
See also: keyboard_needs_poll, install_keyboard, key, key_shifts.
Examples using this: excamera, exsample, exstars.
See also: poll_keyboard, install_keyboard, key.
if (key[KEY_SPACE]) printf("Space is pressed\n");
Note that the array is supposed to represent which keys are physically held down and which keys are not, so it is semantically read-only.
These are the keyboard scancodes:
KEY_A ... KEY_Z, KEY_0 ... KEY_9, KEY_0_PAD ... KEY_9_PAD, KEY_F1 ... KEY_F12, KEY_ESC, KEY_TILDE, KEY_MINUS, KEY_EQUALS, KEY_BACKSPACE, KEY_TAB, KEY_OPENBRACE, KEY_CLOSEBRACE, KEY_ENTER, KEY_COLON, KEY_QUOTE, KEY_BACKSLASH, KEY_BACKSLASH2, KEY_COMMA, KEY_STOP, KEY_SLASH, KEY_SPACE, KEY_INSERT, KEY_DEL, KEY_HOME, KEY_END, KEY_PGUP, KEY_PGDN, KEY_LEFT, KEY_RIGHT, KEY_UP, KEY_DOWN, KEY_SLASH_PAD, KEY_ASTERISK, KEY_MINUS_PAD, KEY_PLUS_PAD, KEY_DEL_PAD, KEY_ENTER_PAD, KEY_PRTSCR, KEY_PAUSE, KEY_ABNT_C1, KEY_YEN, KEY_KANA, KEY_CONVERT, KEY_NOCONVERT, KEY_AT, KEY_CIRCUMFLEX, KEY_COLON2, KEY_KANJI, KEY_LSHIFT, KEY_RSHIFT, KEY_LCONTROL, KEY_RCONTROL, KEY_ALT, KEY_ALTGR, KEY_LWIN, KEY_RWIN, KEY_MENU, KEY_SCRLOCK, KEY_NUMLOCK, KEY_CAPSLOCK KEY_EQUALS_PAD, KEY_BACKQUOTE, KEY_SEMICOLON, KEY_COMMAND
Finally, you may notice an `odd' behaviour of the KEY_PAUSE key. This key only generates an interrupt when it is pressed, not when it is released. For this reason, Allegro pretends the pause key is a `state' key, which is the only way to make it usable.
See also: install_keyboard, poll_keyboard, key_shifts.
Examples using this: Available Allegro examples.
KB_SHIFT_FLAG KB_CTRL_FLAG KB_ALT_FLAG KB_LWIN_FLAG KB_RWIN_FLAG KB_MENU_FLAG KB_COMMAND_FLAG KB_SCROLOCK_FLAG KB_NUMLOCK_FLAG KB_CAPSLOCK_FLAG KB_INALTSEQ_FLAG KB_ACCENT1_FLAG KB_ACCENT2_FLAG KB_ACCENT3_FLAG KB_ACCENT4_FLAG
Example:
if (key[KEY_W]) { if (key_shifts & KB_SHIFT_FLAG) { /* User is pressing shift + W. */ } else { /* Hmmm... lower case W then. */ } }
See also: install_keyboard, poll_keyboard, key.
Examples using this: excamera, exkeys.
while (!keypressed()) { /* Show cool animated logo. */ } /* So he skipped our title screen. */
See also: install_keyboard, readkey, ureadkey, clear_keybuf, simulate_keypress, simulate_ukeypress.
Examples using this: Available Allegro examples.
The low byte of the return value contains the ASCII code of the key, and the high byte the scancode. The scancode remains the same whatever the state of the shift, ctrl and alt keys, while the ASCII code is affected by shift and ctrl in the normal way (shift changes case, ctrl+letter gives the position of that letter in the alphabet, eg. ctrl+A = 1, ctrl+B = 2, etc). Pressing alt+key returns only the scancode, with a zero ASCII code in the low byte. For example:
This function cannot return character values greater than 255. If you need to read Unicode input, use ureadkey() instead.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");
See also: install_keyboard, ureadkey, keypressed, clear_keybuf, simulate_keypress.
Examples using this: Available Allegro examples.
You should be able to find Unicode character maps at http://www.unicode.org/. Remember that on DOS you must specify a custom keyboard map (like those found in `keyboard.dat') usually with the help of a configuration file specifying the language mapping (keyboard variable in system section of `allegro.cfg'), or you will get the default US keyboard mapping.int val, scancode; ... val = ureadkey(&scancode); if (val == 0x00F1) allegro_message("You pressed n with tilde\n"); if (val == 0x00DF) allegro_message("You pressed sharp s\n");
See also: install_keyboard, readkey, keypressed, clear_keybuf, simulate_ukeypress.
Examples using this: exkeys.
Example:
int ascii; ... ascii = scancode_to_ascii(scancode); allegro_message("You pressed '%c'\n", ascii);
See also: scancode_to_name.
char const *keyname = scancode_to_name(scancode); allegro_message("You pressed the %s key.", keyname);
See also: scancode_to_ascii.
Examples using this: exkeys.
simulate_keypress(KEY_SPACE << 8); if (readkey() == (KEY_SPACE << 8)) allegro_message("You simulated Alt+Space\n");
See also: install_keyboard, simulate_ukeypress, keypressed, readkey.
/* We ignore the scancode simulation. */ simulate_ukeypress(0x00DF, 0); if (ureadkey(&scancode) == 0x00DF) allegro_message("You simulated sharp s\n");
See also: install_keyboard, simulate_keypress, keypressed, ureadkey.
int enigma_scrambler(int key) { /* Add one to both the scancode and ascii values. */ return (((key >> 8) + 1) << 8) | ((key & 0xff) + 1); } END_OF_FUNCTION(enigma_scrambler) ... install_timer(); LOCK_FUNCTION(enigma_scrambler); install_keyboard(); keyboard_callback = enigma_scrambler;
Note that this callback will be ignored if you also set the unicode keyboard callback.
See also: install_keyboard, readkey, ureadkey, keyboard_ucallback, keyboard_lowlevel_callback.
int silence_g_key(int key, int *scancode) { if (key == 'g') { *scancode = 0; return 0; } return key; } END_OF_FUNCTION(silence_g_key) ... install_timer(); LOCK_FUNCTION(silence_g_key); install_keyboard(); keyboard_ucallback = silence_g_key;
Note that this keyboard callback has priority over the non unicode callback. If you set both, only the unicode one will work.
See also: install_keyboard, readkey, ureadkey, keyboard_callback, keyboard_lowlevel_callback.
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: install_keyboard, keyboard_callback, keyboard_ucallback.
Examples using this: exkeys.
Note that the led behaviour cannot be guaranteed on some platforms, some leds might not react, or none at all. Therefore you shouldn't rely only on them to communicate information to the user, just in case it doesn't get through./* Cycle led indicators. */ set_leds(KB_SCROLOCK_FLAG); rest(1000); set_leds(KB_CAPSLOCK_FLAG); rest(1000); set_leds(KB_NUMLOCK_FLAG); rest(1000); set_leds(-1);
See also: install_keyboard, key_led_flag.
See also: install_keyboard, readkey, ureadkey.
See also: install_keyboard, keypressed, readkey, ureadkey.
Examples using this: Available Allegro examples.
See also: install_keyboard.
See also: install_keyboard, set_leds.