Fancy String Input using Conio


/ Published in: C
Save to your folder(s)

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.


Copy this code and paste it in your HTML
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5.  
  6.  
  7. /* using sleep function in windoze */
  8. #include <windows.h>
  9. #define yieldcpu() Sleep(0)
  10. /* Works when using deprecated stdlib _sleep */
  11.  
  12. /* quit = 1 when user presses escape key */
  13. unsigned char quit=0;
  14.  
  15. /* this is the buffer for input */
  16. #define maxbuf 256
  17. char buf[maxbuf];
  18.  
  19. /* what happened to cputs? */
  20. #define cputs(a) printf(a)
  21.  
  22.  
  23.  
  24. #ifndef NOCONIO
  25.  
  26. #include <conio.h>
  27.  
  28.  
  29. void repeatchar(char c,int n){
  30. while(n--)putch(c);
  31. }
  32.  
  33.  
  34. void newline(){
  35. putch('\r');
  36. putch('\n');
  37. }
  38.  
  39.  
  40. unsigned short getkey(){
  41. unsigned char c;
  42. while(!kbhit())yieldcpu();
  43. c=getch();
  44. if(c==27)quit=1;
  45. if(c==224)return 256+getkey();
  46. return c;
  47. }
  48.  
  49.  
  50. void pause(){
  51. cputs("Press any key");
  52. getkey();
  53. newline();
  54. }
  55.  
  56.  
  57. void getstring(){
  58. unsigned short c;
  59. unsigned char ox=0;
  60. unsigned char x=0;
  61. unsigned char h=0;
  62. while(!quit){
  63. buf[h]=0;
  64. putch('\r');//use gotox instead if you want something before input
  65. cputs(buf);
  66. putch(' ');
  67. repeatchar(8,h-x+1);//using instead of gotox(x), puts cursor at correct position
  68. c=getkey();
  69. if(quit)break;
  70. switch(c){
  71. default:
  72. if(h>=maxbuf-1)continue;
  73. if(x<h){
  74. memmove(buf+x+1,buf+x,h-x);
  75. ++h;
  76. }
  77. buf[x]=c;
  78. ++x;
  79. if(x>h)h=x;
  80. continue;
  81.  
  82. case 8://backspace
  83. if(x){
  84. ox=x;
  85. --x;
  86. Del:
  87. if(x<h){
  88. memmove(buf+x,buf+ox,h-ox);
  89. --h;
  90. }
  91. }
  92. continue;
  93.  
  94. case 339://delete key
  95. ox=x+1;
  96. goto Del;
  97.  
  98. case 331://left arrow
  99. if(x){
  100. --x;
  101. }
  102. continue;
  103.  
  104. case 333://right arrow
  105. if(x<h){
  106. ++x;
  107. }
  108. continue;
  109.  
  110. case 327://home
  111. x=0;
  112. continue;
  113.  
  114. case 335://end
  115. x=h;
  116. continue;
  117.  
  118. case 13://enter
  119. newline();
  120. return;
  121. }
  122. }
  123. }
  124.  
  125.  
  126. #else
  127.  
  128.  
  129. void newline(){
  130. putchar('\r');
  131. putchar('\n');
  132. }
  133.  
  134.  
  135. void getstring(){
  136. fgets(buf,maxbuf,stdin);
  137. buf[strlen(buf)-1]=0;/* get rid of newline */
  138. }
  139.  
  140.  
  141. void pause(){
  142. puts("Press Enter");
  143. }
  144.  
  145.  
  146. #endif
  147.  
  148.  
  149.  
  150. /* demo program */
  151. int main(){
  152. printf("Wat's ur name?");
  153. newline();
  154. getstring();
  155. printf("Heya, %s!",buf);
  156. newline();
  157. pause();
  158. return 0;
  159. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.