Turbo C - Reading Arrow Keys with bioskey
We can use function keys F1, F2, F3, etc and any control keys and Up Arrow / Down Arrow in Turbo C.
We have to efficient function call bioskey rather than getch(). bioskey(1) to read whether a key is pressed or not. bioskey(0) to read the actual key.
Source code and Sample output is given below
Source Code
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <bios.h>
#include <ctype.h>
#define F1_Key 0x3b00
#define F2_Key 0x3c00
#define F3_Key 0x3d00
#define F4_Key 0x3e00
#define F5_Key 0x3f00
#define F6_Key 0x4000
#define F7_Key 0x4100
#define F8_Key 0x4200
#define F9_Key 0x4300
#define F10_Key 0x4400
#define LEFT_ARROW_Key 0x4B00
#define RIGHT_ARROW_Key 0x4D00
#define UP_ARROW_Key 0x4800
#define DOWN_ARROW_Key 0x5000
int handle_keyevents()
{
int key = bioskey(0);
if (isalnum(key & 0xFF))
{
printf("'%c' key pressed\n", key);
return 0;
}
switch(key)
{
case F1_Key:
printf("F1 Key Pressed");
break;
case F2_Key:
printf("F2 Key Pressed");
break;
case F3_Key:
printf("F3 Key Pressed");
break;
case F4_Key:
printf("F4 Key Pressed");
break;
case F5_Key:
printf("F5 Key Pressed");
break;
case F6_Key:
printf("F6 Key Pressed");
break;
case F7_Key:
printf("F7 Key Pressed");
break;
case F8_Key:
printf("F8 Key Pressed");
break;
case F9_Key:
printf("F9 Key Pressed");
break;
case LEFT_ARROW_Key:
printf("Left Arrow Key Pressed");
break;
case RIGHT_ARROW_Key:
printf("Right Arrow Key Pressed");
break;
case UP_ARROW_Key:
printf("UP Arrow Key Pressed");
break;
case DOWN_ARROW_Key:
printf("Left Arrow Key Pressed");
break;
case F10_Key:
printf("F10 Key Pressed");
return -1;
default:
printf("%#02x\n", key);
break;
}
printf("\n");
return 0;
}
void main()
{
int key;
printf("Press F10 key to Quit\n");
while(1)
{
key = bioskey(1);
if(key > 0)
{
if(handle_keyevents() < 0)
break;
}
}
}
Output
Press F10 key to Quit
'a' key pressed
's' key pressed
'd' key pressed
'f' key pressed
'g' key pressed
'f' key pressed
'L' key pressed
'Q' key pressed
'R' key pressed
'E' key pressed
'Y' key pressed
'U' key pressed
'R' key pressed
'J' key pressed
'3' key pressed
'4' key pressed
'6' key pressed
Left Arrow Key Pressed
Right Arrow Key Pressed
Up Arrow Key Pressed
Down Arrow Key Pressed
F1 Key Pressed
F2 Key Pressed
F5 Key Pressed
F6 Key Pressed
F6 Key Pressed
F7 Key Pressed
F8 Key Pressed
F8 Key Pressed
F10 Key Pressed
|