Question

I have written the below program in C. It is running perfectly (windows 7, turbo c++). The program is to select the number using up arrow and down arrow key's and to print it in words.

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

int main(){
    int x,i=2;

g:
   clrscr();
   printf("Print");
   printf("\n 1");
   printf("\n 2");
   printf("\n EXIT");
   printf("\nChoose using arrow key");
   gotoxy(1,i);

while((x=bioskey(0))!=75) // I took a value so that the loop continues. 
{                         // no other reason for 75      
  if(x==20480){
    if(i<=3){
    gotoxy(1,i+1);
    i=i+1;
    }else{}
  }else if(x==18432){
              if(i==2){}else{
              gotoxy(1,i-1);
              i=i-1;}
  }else if(x==7181){
          clrscr();
          if(i==2){
          printf("ONE ");
          }else if(i==3){
          printf("TWO ");
              }else{
          printf("thank you");
          getch();
              return 0;
              }
    getch();
    goto g;
}else{}

}

 getch();
 return 0;
}

Is there any method easier than this to achieve the same functionality using arrow keys?

Also help me to highlight the selected row ( the row in which cursor blinks).

One more question : While " # " is typed there is a color change displayed on the screen. How to change the color of alphabets similarly ? Please do not tell answer using <GRAPHICS.H>

Was it helpful?

Solution 2

May be this code works.

Warning This code is not compiled/tested.

int highlight_option(int index)
{
    int i;
    char menu[4][6] = {"Print", "1", "2", "Exit"};
    clrscr();
    for (i = 0; i < 4; i++) {
        if (i == index) {
            textbackground(WHITE);
            textcolor(BLACK);
            cprintf("%s\n", menu[i]);
            textbackground(BLACK);
            textcolor(WHITE);
        } else {
            printf("%s\n", menu[i]);
        }
    }
    return 0;
}

Call this function from the arrow handler, pass i + 1 or i - 1 to this function before the gotoxy

OTHER TIPS

Anyone in need of answer later may refer this also. A simple example for selection using arrow key, and highlighting.

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

void disp(int n)
{
int i=0;
char menu[4][20]={"PRINT","1","2","EXIT"};

clrscr();
gotoxy(1,1);
for(i=0;i<4;i++){

    if(i==n){
    textbackground(WHITE);
    textcolor(RED);
    gotoxy(1,i+1);
    cprintf("%s\n",menu[i]);
    textbackground(BLACK);
    textcolor(WHITE);
    }else{
    gotoxy(1,i+1);
    textcolor(WHITE);
    cprintf("%s\n",menu[i]);
    }
        }

}

int main()
{
 int x,i=2;
 disp(1);
 gotoxy(1,i);


 while((x=bioskey(0))!=75)
 {

 gotoxy(1,i);
    if(x==20480){
        if(i<=3){
            i=i+1;
            disp(i-1);
            gotoxy(1,i);
            }else{}
    }else if(x==18432){
            if(i==2){}else{
        i=i-1;
            disp(i-1);
            gotoxy(1,i);
            }
    }else if(x==7181){
        clrscr();
            if(i==2){
            printf("ONE");
            }else if(i==3){
            printf("TWO");
            }else{
            printf("Thank You");
            getch();
            return 0;
            }
    }else{
    disp(i-1);
    gotoxy(1,i);
    }
 }

return 0;
}

Use bioskey to read the UP and DN arrow keys. Refer to the following code:

#include <stdio.h>
#include <graphics.h>
#include <conio.h>
#include <bios.h>


#define LTARROW 0x4B 
#define RTARROW 0x4D 
#define UPARROW 0x48 
#define DNARROW 0x50 
#define CR 0x0d 
#define ESC 0x1b 

#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


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 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;
            }
      }
}

Reference:

http://www.softwareandfinance.com/Turbo_C/bioskey.html

http://www.softwareandfinance.com/Turbo_C/Graphics_MoveObject.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top