Is there a better way to "read" the arrow keys than using KbdCharIn? It doesn't work like in DOS where there are TWO bytes returned.
/ Peter Schuller [TeamOS/2] pete...@kuai.se
>Is there a better way to "read" the arrow keys than using KbdCharIn? It doesn't work like in DOS where there are TWO bytes returned.
Actually, KbdCharIn does return two bytes; it just doesn't
return them in the obvious place.
Here's the code that I use to read the keyboard. It's
written in Modula-2, but it should be obvious how to
translate it into whatever language you're using.
PROCEDURE InputTask;
(* Runs as a separate thread. Picks up the keyboard *)
(* input and stores it in CharBuffer. *)
VAR KeyData: OS2.KBDKEYINFO;
result: CHAR;
BEGIN
LOOP
OS2.KbdCharIn (KeyData, 0, 0);
result := KeyData.chChar;
IF (result = CHR(0)) OR (result = CHR(224)) THEN
PutCode (TRUE, KeyData.chScan);
ELSE
PutCode (FALSE, result);
END (*IF*);
END (*LOOP*);
END InputTask;
Here, PutCode is a procedure that stores its second
argument in a circular buffer; except that, if the first argument
is TRUE, then it stores a two-character sequence where
the first character is a null character. As a result,
anyone reading from the circular buffer gets back the
codes just the way that DOS would return them.
The trick here is that the "chScan" field doesn't hold
a scan code, as you would expect. Instead, it holds that
second character you're looking for.
(And I use a counting semaphore to keep track of how many
characters are stored in the circular buffer.)
--
Peter Moylan pe...@ee.newcastle.edu.au
http://www.ee.newcastle.edu.au/users/staff/peter/Moylan.html
OS/2 freeware list at
http://www.ee.newcastle.edu.au/users/staff/peter/os2/os2info.html
PS> Is there a better way to "read" the arrow keys than using
PS> KbdCharIn? It doesn't work like in DOS where there are TWO
PS> bytes returned.
If the (first) byte is 0 or 224, the second byte is waiting to be
picked up. Also, kbdkeyinfo.status seems to be able to give some
information about a second byte, too.
For example (using _kbhit() instead of KbdCharIn()) :
if (_kbhit())
ch1=_getch();
if (ch1==0 || ch1==224)
{
ch2=_getch();
printf("Bytes read : %d %d\n",ch1,ch2);
}
else
{
printf("Byte read : %d\n",ch1);
}
Ave,
Andre Doff
ad...@ibm.net