Thursday, February 19, 2015

Learning C Functions :_bios_keybrd ( Dos Only )

C Programming :
Function :
_bios_keybrd

Syntax

#include <bios.h>
unsigned _bios_keybrd(unsigned cmd);

Description


_bios_keybrd performs various keyboard operations using BIOS interrupt 0x16. The parameter cmd determines the exact operation .

Return Value

The value returned by _bios_keybrd depends on the task it performs, determined by the value of cmd (defined in bios.h):

_KEYBRD_READ    If the lower 8 bits are nonzero, _bios_keybrd returns the ASCII character for the next keystroke waiting in the queue
or the next key pressed at the keyboard. If the lower 8 bits are zero, the upper 8 bits are the extended keyboard codes defined in the IBM PC Technical Reference Manual.
_NKEYBRD_READ    Use this value instead of _KEYBRD_READY to read the keyboard codes for enhanced keyboards, which have additional cursor and function keys.
_KEYBRD_READY    This tests whether a keystroke is available to be read. A return value of zero means no key is available. The return value is 0xFFFF (-1) if Ctrl-Brk has been pressed. Otherwise, the value of the next keystroke is returned, as described in _KEYBRD_READ (above). The keystroke itself is kept to be returned by the next call to _bios_keybrd
that has a cmd value of _KEYBRD_READ or _NKEYBRD_READ   
_NKEYBRD_READY    Use this value to check the status of enhanced keyboards, which have additional cursor and function keys.

_KEYBRD_SHIFTSTATUS    Requests the current shift key status. The value will contain an OR of zero or more of the following values:

     Bit 7    0x80    Insert on
     Bit 6    0x40    Caps on
     Bit 5    0x20    Num Lock on
     Bit 4    0x10    Scroll Lock on
     Bit 3    0x08    Alt pressed
     Bit 2    0x04    Ctrl pressed
     Bit 1    0x02    Left Shift pressed
     Bit 0    0x01    Right Shift pressed

_NKEYBRD_SHIFTSTATUS    Use this value instead of _KEYBRD_SHIFTSTATUS to request the full 16-bit shift key status for enhanced
keyboards. The return value will contain an OR of zero or more of the bits defined above in
_KEYBRD_SHIFTSTATUS, and additionally, any of the following bits:

     Bit 15    0x8000    Sys Req pressed
     Bit 14    0x4000    Caps Lock pressed
     Bit 13    0x2000    Num Lock pressed
     Bit 12    0x1000    Scroll Lock pressed
     Bit 11    0x0800    Right Alt pressed
     Bit 10    0x0400    Right Ctrl pressed
     Bit 9    0x0200    Left Alt pressed
     Bit 8    0x0100    Left Ctrl pressed

Example :

/*  _bios_keybrd example  */

#include <stdio.h>
#include <bios.h>
#include <ctype.h>

#define RIGHT  0x01
#define LEFT   0x02
#define CTRL   0x04
#define ALT    0x08

int main(void)
{
   int key, modifiers;

   /*  Wait until a key is pressed  */
   while (_bios_keybrd(_KEYBRD_READY) == 0);

   /*  Fetch the key that is waiting  */
   key = _bios_keybrd(_KEYBRD_READ);

   /*  Determine if shift keys are used  */
   modifiers = _bios_keybrd(_KEYBRD_SHIFTSTATUS);
   if (modifiers){
      printf("[");
      if (modifiers & RIGHT) printf("RIGHT");
      if (modifiers & LEFT)  printf("LEFT");
      if (modifiers & CTRL)  printf("CTRL");
      if (modifiers & ALT)   printf("ALT");
      printf("]");
   }

   /*  print out the character read  */
   if (isalnum(key & 0xFF))
      printf("'%c'\n", key);
   else
      printf("%#02x\n", key);
   return 0;
}

No comments:

Post a Comment