Path: utzoo!attcan!darkover!nebulus!druid!darcy From: darcy@druid.uucp (D'Arcy J.M. Cain) Newsgroups: comp.sys.ibm.pc Subject: Re: MS-DOS call to read without echo Message-ID: <1990Feb9.000950.11693@druid.uucp> Date: 9 Feb 90 00:09:50 GMT References: <1262@srhqla.SR.COM> Reply-To: darcy@druid.UUCP (D'Arcy J.M. Cain) Organization: D'Arcy Cain Consulting, West Hill, Ontario Lines: 67 In article <1262@srhqla.SR.COM> tcm@srhqla.SR.COM (Tim Meighan) writes: >/* > * GET_KEY() checks for IBM-PC keyboard input, returning a -1 if no character > * is available, 0 to 255 for regular keypresses, and values above 255 for > * function keypresses. Note that the function kbhit() is not from the > * standard library but is part of MSC. Use this function ONLY when > * standard input will be the PC keyboard. > */ > >int get_key() >{ > int c; > > if(kbhit()) > { > if((c = getch()) == 0) > { > c = (getch() + 197); > return(c); > } > } > return(-1); >} > >Tim Meighan >Silent Radio Hmmm. Seems to only allow for function keys to be entered. Regular keys will return -1. Also 197 isn't the best choice. Here is how I did the same thing: int get_key() { int c; if(kbhit()) { if ((c = getch()) == 0) c = (getch() << 8); return(c); } return(0); } By doing the shift you get two advantages. First, the return value can be anded with 0xff00 and tested for special characters. Second, the return value winds up looking like the scan code return and it is easier to enter a table of function keys into a #define list from standard texts. I also used 0 to signify no character since it is an impossible return. It is slightly more intuitive and can save a memory fetch and a byte when comparing the return such as: if (c = get_key) process_key(); instead of if ((c = get_key()) != -1) process_key(); -- D'Arcy J.M. Cain (darcy@druid) | Thank goodness we don't get all D'Arcy Cain Consulting | the government we pay for. West Hill, Ontario, Canada | (416) 281-6094 |