char * UserInput(int max) { BOOL EndInput = TRUE; unsigned int Count; int Count2 = 0, Count3 = 0, cursore = 0; char AllowedCh[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', '\0'}; char AllowedNum[] = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '\0'}; char *Buff; char Current = ' '; char provvisorio[2]; char *definitivo; Buff = (char *) malloc((max + 1)); definitivo = (char *) malloc((max + 1)); Buff[0] = '\0'; definitivo[0] = '\0'; while(EndInput) { provvisorio[Count3] = getch(); definitivo = strupr(provvisorio); definitivo[Count3 + 1] = '\0'; Current = definitivo[Count3]; switch(Current) { case 13: EndInput = FALSE; Buff[Count2] = '\0'; break; case 8: Buff[Count2] = '\0'; if(cursore > 0) { printf("\b \b"); Count2--; Buff[Count2] = '\0'; cursore--; } break; /*function keys exclusion*/ case(0+65): break; case(0+66): break; case(0+67): break; case(0+68): break; case(0+69): break; case(0+70): break; case(0+71): break; case(0+72): break; case(0+73): break; case(0+74): break; /****************/ default: if(max == 1) { for(Count = 0; Count < strlen(AllowedCh); Count++) { if(Current == AllowedCh[Count] && (Count2 < max)) { printf("%c", Current); Buff[Count2] = Current; Count2++; cursore++; } } break; } else { for(Count = 0; Count < strlen(AllowedNum); Count++) { if(Current == AllowedNum[Count] && (Count2 < max)) { printf("%c", Current); Buff[Count2] = Current; Count2++; cursore++; } } break; } } } return Buff; } //End UserInput Code (markup): I have a problem with this code, if I press F7, F8, F9, F10, pagup, home introduce valid character for input. Well have you some suggestion about how to recognize if a function key or a char key is pressed and accept only the chars? How to do in my code? Example, if I press F7 it will input A. I don't want this!
Hmm.. If i remember correctly, when getch() is used, function keys send two codes instead of one. First a 0 is returned from getch() (which is an indication this is an extended key), and then a special code is returned (which causes the A to be printed). So, all you need to do is make a check that if getch() returns a zero, then call it again to get the actual code of the key.