I want the user to use the four cursors up,down,right,left.
i tried to use getch() but it didnt work because the four have the same ASCII code which is 224, so i cant diffrentiate between them
plz any one tell me how to make the user use them
aam and another thing
i want what i display on the screen to be bold
like DANI WEB How can i do it
plz reply me
thnx

Dani AI

Generated

asked how to read the arrow keys and how to make console text bold. ’s getch-based snippet is a solid, pragmatic Windows/DOS console solution, and ’s memory of historical DOS behaviour is on point. For broader, more portable control there are three practical approaches depending on the target environment.

Use the native console APIs for full control on Windows (console input events give reliable key codes and modifiers). On Unix-like systems prefer a terminal library such as ncurses: it takes care of raw mode, maps arrow keys to symbolic constants, and handles screen updates. Minimal ncurses example:

#include <ncurses.h>

int main(void) {
  initscr();
  cbreak();
  noecho();
  keypad(stdscr, TRUE);
  int ch;
  while ((ch = getch()) != 'q') {
    if (ch == KEY_UP)    printw("UP\n");
    else if (ch == KEY_DOWN)  printw("DOWN\n");
    else if (ch == KEY_LEFT)  printw("LEFT\n");
    else if (ch == KEY_RIGHT) printw("RIGHT\n");
    refresh();
  }
  endwin();
  return 0;
}

If not using a library, arrow keys arrive as multi-byte escape sequences in many terminals (they start with ESC). That requires switching the terminal to raw mode (termios) and reading bytes with a short timeout so a single ESC keypress isn’t mistaken for an escape sequence.

For bold text: most terminal emulators accept ANSI SGR codes, e.g. \x1b[1m to enable bold/bright and \x1b[0m to reset:

printf("\x1b[1mDANI WEB\x1b[0m\n");

On classic Windows consoles use the Win32 console attributes (GetConsoleScreenBufferInfo / SetConsoleTextAttribute) to set intensity/colour and restore the original attributes afterwards. Choose the method that matches the deployment target: conio for quick Windows-only hacks, ncurses/PDCurses for cross-platform console UIs, or native console APIs for full-featured Windows programs.

Recommended Answers

All 3 Replies

Try something like this:

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

/* System dependent key codes */
enum
{
  KEY_ESC     = 27,
  ARROW_UP    = 256 + 72,
  ARROW_DOWN  = 256 + 80,
  ARROW_LEFT  = 256 + 75,
  ARROW_RIGHT = 256 + 77
};

static int get_code ( void )
{
  int ch = getch();

  if ( ch == 0 || ch == 224 )
    ch = 256 + getch();

  return ch;
}

int main ( void )
{
  int ch;

  while ( ( ch = get_code() ) != KEY_ESC ) {
    switch ( ch ) {
    case ARROW_UP:
      printf ( "UP\n" );
      break;
    case ARROW_DOWN:
      printf ( "DOWN\n" );
      break;
    case ARROW_LEFT:
      printf ( "LEFT\n" );
      break;
    case ARROW_RIGHT:
      printf ( "RIGHT\n" );
      break;
    }
  }

  return 0;
}

Hey, I used to know that arrow keys(left, right, up and down) return ASCII 0 with getch(), then u have to use getch() again to get the value which is like 72, 80 etc.
My experience was limited to DOS, i would like to know which system u were working on that returned 224 first time. In my very first introductory CS class i asked my lecturer why does the arrow keys(and some others) return 0. I know the reason why it is not possible to return an ASCII value but why "0"? Then he barked at me before the whole class, "NOW WHY ARE U ASKING THIS KIND OF QUESTIONS, HOW AM I SUPPOSED KNOW THAT? U GO ASK THE PERSON WHO MADE THIS COMPUTER".

Can any one plz enlighten me with this piece of knowledge?

>i would like to know which system u were working on that returned 224 first time.
The good news is that by the mention of getch (and no mention of curses) you can assume either Windows or DOS. So if it's not DOS proper, it must be a Windows version. ;) Booting into Windows and testing the first return of getch confirms this assumption.

>HOW AM I SUPPOSED KNOW THAT?
Silly teacher. He could have at least guessed that it was an arbitrary extension code that was conveniently a not often used value. :rolleyes:

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.