Thursday, February 19, 2015

Learning C Functions : bioskey ( Dos Only )

C Programming :
Function
:
bioskey

Syntax

#include <bios.h>
int bioskey(int cmd);

Description

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

Return Value

The value returned by bioskey depends on the task it performs, determined by the value of cmd:

0    If the lower 8 bits are nonzero, bioskey 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.
1    This tests whether a keystroke is available to be read. A return value of zero means no key is available. The return value is 0xFFFFF (-1) if Ctrl-Brk
has been pressed. Otherwise, the value of the next keystroke is returned. The keystroke itself is kept to be returned by the next call to bioskey that has a cmd value of zero.
2    Requests the current shift key status. The value is obtained by ORing the following values together:

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 arrow + Shift pressed
Bit 0    0x01    Right arrow + Shift pressed
 
Example :

/* bioskey 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;

   /* function 1 returns 0 until a key is pressed */
   while (bioskey(1) == 0);

   /* function 0 returns the key that is waiting */
   key = bioskey(0);

   /* use function 2 to determine if shift keys were used */
   modifiers = bioskey(2);
   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