Good day, i'm new in C.. can you give me some idea in how to program a moving cursor in the output screen and highlight the text... because i will use this in my coming final project in school.. we will make a system in TURBO C..

hoping for reply... THANK YOU!

Dani AI

Generated

If you want a Turbo C text UI that moves a highlight with the arrow keys, and are on the right track: use conio.h for cursor and colors, then draw using the direct-video functions. Two key gotchas that trip folks up:

  • gotoxy uses 1-based coordinates (1..cols, 1..rows).
  • textcolor/textbackground affect direct console output (for example cprintf), not buffered stdio like printf, and they do not retroactively recolor text already printed. (programmers-lounge.blogspot.com)

Arrow keys are "extended" keys: call getch() twice. The first call returns 0 or 0xE0, and the second returns the code (Left=75, Right=77, Up=72, Down=80). That pattern is consistent with the classic DOS/CRT behavior. (learn.microsoft.com)

Here is a small, self-contained demo that moves and highlights a label without busy-wait loops or full-screen clears. It also clamps movement to the screen using gettextinfo:

#include <conio.h>
#include <string.h>

int main(void) {
    struct text_info ti; gettextinfo(&ti);
    const char *label = "SELECT";
    int len = strlen(label);
    int x = 10, y = 10, px = x, py = y, ch, i;

    clrscr();
    for (;;) {
        // erase previous
        gotoxy(px, py); textbackground(BLACK); textcolor(LIGHTGRAY);
        for (i = 0; i < len; ++i) cprintf(" ");

        // draw highlight at new position
        gotoxy(x, y); textbackground(LIGHTBLUE); textcolor(WHITE);
        cprintf("%s", label); normvideo();

        px = x; py = y;

        ch = getch();
        if (ch == 27) break;               // ESC quits
        if (ch == 0 || ch == 0xE0) {
            ch = getch();
            if (ch == 75 && x > 1) x--;                           // left
            else if (ch == 77 && x < ti.screenwidth - len + 1) x++; // right
            else if (ch == 72 && y > 1) y--;                       // up
            else if (ch == 80 && y < ti.screenheight) y++;         // down
        }
    }
    return 0;
}

If you’re tempted to use ANSI escape sequences instead, remember they require an ANSI driver (ANSI.SYS) to be loaded under DOS and were historically slow; conio routines are simpler and faster in this environment, which echoes ’s advice. (en.wikipedia.org)

Recommended Answers

All 12 Replies

In Turbo C you can use gotoxy(), and for the highlighting you'd probably use textcolor(). Read the documentation for further details about those two functions.


inb4 don't use Turbo C.

In Turbo C you can use gotoxy(), and for the highlighting you'd probably use textcolor(). Read the documentation for further details about those two functions.


inb4 don't use Turbo C.

Thank You for tha Information sir... but my problem that i can move/manipulate the cursor left, right, down, up in the output screen.

Hope you can help me...

but my problem that i can move/manipulate the cursor left, right, down, up in the output screen.

I don't understand.

Thank You for tha Information sir... but my problem that i can move/manipulate the cursor left, right, down, up in the output screen.

Hope you can help me...

He already told you how -- call gotoxy().

hope this help:

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
long k;
clrscr();
for(i=0;i<=300;i++)
{
clrscr();
for(j=0;j<=i;j++)
{
printf(" ");
  }
//printf("abc");
for(k=1;k<=900000;k++);//increasing this value will slow down the cursor speed.
   }              
getch();
}

>>for(k=1;k<=900000;k++);//increasing this value will slow down the cursor speed.

Horrible waste of CPU time! That is dependent on CPU speed. In Turbo C call its delay() function.

Deciptikon, Ancient Dragon, and student learner... thank you for the reply. When i was surfing in the net i come with this site "".. this is what i mean.. can you teach me how to do that "ANSI escape" or give some sample program...

Thank You so much...

You *might* be able to use ANSI escape sequences by . But it's a really bad solution to the problem when Turbo C already supports such library functions as gotoxy() and textcolor().

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

#define d1 gotoxy(1,1);textcolor(RED);cprintf(" P");textcolor(7);cprintf("relim       ");
#define d2 gotoxy(1,6);textcolor(RED);cprintf(" M");textcolor(7);cprintf("idterm      ");
#define d3 gotoxy(1,12);textcolor(RED);cprintf(" S");textcolor(7);cprintf("emi finals  ");
#define d4 gotoxy(1,18);textcolor(RED);cprintf(" F");textcolor(7);cprintf("inals       ");
#define keys gotoxy(1,24);printf("Esc = exit\tArrow keys = up/down/left/right");
#define d clrscr(); keys d1 d2 d3 d4

int c,cx=0,cy=0,i1,i2,i3,i4,i5,i6,i7; /*some variables will be use --in progress -*/ 
char c1,c2,c3,c4,c5,c6,c7,c8;
float f1,f2;

void updown()
  {
  switch(c)
    {
    case  72 : cy = cy - 1;
	       break;
    case  80 : cy = cy + 1;
	       break;
    default  : printf("");
    }
  }



void b2()
  {
  switch(cx)
    {
    case  1 : gotoxy(20,1);printf("PRELIM\n");
	      gotoxy(20,2);printf("PROGRAM 1\n");
	      gotoxy(20,3);printf("PROGRAM 2\n");
	      gotoxy(20,4);printf("PROGRAM 3\n");
	      break;
    case  2 : gotoxy(20,1);printf("MIDTERM\n");
	      gotoxy(20,2);printf("PROGRAM 1\n");
	      gotoxy(20,3);printf("PROGRAM 2\n");
	      gotoxy(20,4);printf("PROGRAM 3\n");
	      break;
    case  3 : gotoxy(20,1);printf("SEMI FINALS\n");
	      gotoxy(20,2);printf("PROGRAM 1\n");
	      gotoxy(20,3);printf("PROGRAM 2\n");
	      gotoxy(20,4);printf("PROGRAM 3\n");
	      break;
    case  4 : gotoxy(20,1);printf("FINALS\n");
	      gotoxy(20,2);printf("PROGRAM 1\n");
	      gotoxy(20,3);printf("PROGRAM 2\n");
	      gotoxy(20,4);printf("PROGRAM 3\n");
	      break;
    default : printf("");
    }
  }

void leftright()
  {
  if(c==77 || c==75)
    {
    if(c==77)

      if(cy==1)
      cx=1;
      else if(cy==2)
      cx=2;
      else if(cy==3)
      cx=3;
      else if(cy==4)
      cx=4;

    else if(c==75)
      cx=0;
    }
  else
    cx=0;
  }



void b1()
  {
  if(cy>=5)
    cy=1;
  else if(cy<=-1)
    cy=4;
  switch(cy)
    {
    case  1 : d textbackground(9); d1 textbackground(BLACK); d2 d3 d4
	      break;
    case  2 : d textbackground(9); d2 textbackground(BLACK); d3 d4
	      break;
    case  3 : d textbackground(9); d3 textbackground(BLACK); d4
	      break;
    case  4 : d textbackground(9); d4 textbackground(BLACK);
	      break;
    default : printf("");
    }
    leftright();
    b2();
  }





main()
  {
  d
  do
    {
    c = getch();
    d
    updown();
    b1();
    b2();
    }
  while(c!=0x1b);
  }

TESTING mo brad .. sensya na magulo yung gawa ko hehe 1st day ko plang ginawa e :))
this is our individual project for final
**note** my program is still in progress

to move cursor you need to ch=getch();
in loop;
then you have to just match ch=='P' or 'K','H','M'
left arrow = K
right arrow = M
up arrow = H
down arrow = P
now use gotoxy(x,y)
x++,y
x--,y
x,y++
x,y--

hi cutterpillow20 now I have the same problem as you had. could you get any awnser if yes then help me too. :) thanks

Didn't you read the rest of this thread???? No one can help you if you don't know how to read.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.