Monday 5 May 2014

Cursor Toggling

#include<conio.h>
#include<iostream>
#include<windows.h>
using namespace std;
void gotoxy (int x,int y)
{
    COORD coord;
    coord.X = x; coord.Y = y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
int main()
{
    int i;
    int y=1,x=1;
    cout<<"press arrow keys to move cursor :\n";
    cout<<"press 0 to exit the program :";
     Sleep(3500);
    system("cls");
   
    
    while(1)
    {
    while(!kbhit());
    i=getch();
        if(i==72 )
        {
            y--;
            if(x>0 && y>0)
            {
            gotoxy(x,y);
        }
        if(y<0)
              y++;
           }
        
        else if(i==80)
        {
            y++;
            if(x>0 && y>0)
            gotoxy(x,y);
        }
        else if(i==75)
        {
            x--;
            if(x>0 && y>0)
            {gotoxy(x,y);
            }
if(x<0)
x++;
        }
    
        else if(i==77)
        {
            x++;
            if( x>0 && y>0)
            {
            gotoxy(x,y);
            }
    }
        else if(i=='0')
        break;
    }
    getch();
    return 0;
}

No comments:

Post a Comment