Revision: 20738
                            
                                                            
                                    
                                        
Initial Code
                                    
                                    
                                                            
                                    
                                        
Initial URL
                                    
                                    
                                
                                                            
                                    
                                        
Initial Description
                                    
                                    
                                                            
                                    
                                        
Initial Title
                                    
                                    
                                                            
                                    
                                        
Initial Tags
                                    
                                    
                                
                                                            
                                    
                                        
Initial Language
                                    
                                    
                                                    
                        at November 24, 2009 06:10 by Zufolek
                            
                            Initial Code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* using sleep function in windoze */
#include <windows.h>
#define yieldcpu() Sleep(0)
/* Works when using deprecated stdlib _sleep */
/* quit = 1 when user presses escape key */
unsigned char quit=0;
/* this is the buffer for input */
#define maxbuf 256
char buf[maxbuf];
/* what happened to cputs? */
#define cputs(a) printf(a)
#ifndef NOCONIO
#include <conio.h>
void repeatchar(char c,int n){
 while(n--)putch(c);
}
void newline(){
 putch('\r');
 putch('\n');
}
unsigned short getkey(){
 unsigned char c;
 while(!kbhit())yieldcpu();
 c=getch();
 if(c==27)quit=1;
 if(c==224)return 256+getkey();
 return c;
}
void pause(){
 cputs("Press any key");
 getkey();
 newline();
}
void getstring(){
 unsigned short c;
 unsigned char ox=0;
 unsigned char x=0;
 unsigned char h=0;
 while(!quit){
  buf[h]=0;
  putch('\r');//use gotox instead if you want something before input
  cputs(buf);
  putch(' ');
  repeatchar(8,h-x+1);//using instead of gotox(x), puts cursor at correct position
  c=getkey();
  if(quit)break;
  switch(c){
   default:
   if(h>=maxbuf-1)continue;
   if(x<h){
    memmove(buf+x+1,buf+x,h-x);
    ++h;
   }
   buf[x]=c;
   ++x;
   if(x>h)h=x;
   continue;
   case 8://backspace
   if(x){
    ox=x;
    --x;
    Del:
    if(x<h){
     memmove(buf+x,buf+ox,h-ox);
     --h;
    }
   }
   continue;
   case 339://delete key
   ox=x+1;
   goto Del;
   case 331://left arrow
   if(x){
    --x;
   }
   continue;
   case 333://right arrow
   if(x<h){
    ++x;
   }
   continue;
   case 327://home
   x=0;
   continue;
   case 335://end
   x=h;
   continue;
   case 13://enter
   newline();
   return;
  }
 }
}
#else
void newline(){
 putchar('\r');
 putchar('\n');
}
void getstring(){
 fgets(buf,maxbuf,stdin);
 buf[strlen(buf)-1]=0;/* get rid of newline */
}
void pause(){
 puts("Press Enter");
 getchar();
}
#endif
/* demo program */
int main(){
 printf("Wat's ur name?");
 newline();
 getstring();
 printf("Heya, %s!",buf);
 newline();
 pause();
 return 0;
}
                                Initial URL
Initial Description
Windows console string input allowing use of left and right arrow keys, backspace, and delete. For portability, falls back to stdio functions when you define NOCONIO.
Initial Title
Fancy String Input using Conio
Initial Tags
Initial Language
C