Helloo...
I am making a simple notepad program. I don't know how to move cursor using ASCII Table. I am using Two way doubly Linked List method to insert characters. And file handling to save on file. I don't know how to move cursor on up, down, right and left.
Please Help....

What compiler and operating system are you using? AFAIK the ascii table doesn't have anything that will move the cursor, except for the backspace key.

I am using C-Free 5.0 and Windows 8.1 .....
So what I should do to solve this problem...
I want to use Up, Down, Left & Right Arrows Keys....

getch() returns either 0 or 227 for special keys. When that happens you have to call getch() again to get the actual key code. This is one of the few times I would recommend using non-standard conio.h, but I don't know if your compiler supports it or not. If not, then you will have to use win32 api console functions, which are more complicated.

#include <stdio.h>
#include <conio.h>
#define ESC 27

int main()

{
    int key;
    while ((key = _getch()) != ESC)
    {
        if (key == 0 || key == 224)
        {

            key = _getch();
            printf("%d\n", key);
        }

    }
}

Its working on C-Free but just showing the numbers.....
How to move cursor?????
I have no idea, I am just a new student....
Please help...

Its working on C-Free but just showing the numbers.....
How to move cursor?????
I have no idea, I am just a new student....
Please help...

See the function SetConsoleCursorPosition() from this link

Here is a hint to get you started. First, define the values for the arrow keys, then call SetConsoleCursorPosition() inside a switch statement.

#include <windows.h>
#include <stdio.h>
#include <conio.h>
#define ESC 27

#define UPARROW 72
#define DOWNARROW 80
#define LEFTARROW 75
#define RIGHTARROW 77

int main()

{
    int key;
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD cord = {0,0};

    while ((key = _getch()) != ESC)
    {
        if (key == 0 || key == 224)
        {

            key = _getch();
            switch(key)
            {
                case UPARROW:
                    if( cord.Y > 0)
                    {
                        cord.Y--;
                        SetConsoleCursorPosition(hConsole, cord);
                    }
                    break;
                case DOWNARROW:
                    break;
                case RIGHTARROW:
                    break;
                case LEFTARROW:
                    break;
            }
        }

    }
}

Edited 2 Years Ago by Ancient Dragon

Before doing the above, you have to make sure your compiler has access to windows.h and associated libraries. If not, then you need to download and install the free Windows SDK.

Edited 2 Years Ago by Ancient Dragon

This article has been dead for over six months. Start a new discussion instead.