Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Arrowns - KbdCharIn

0 views
Skip to first unread message

pete...@kuai.se

unread,
Oct 30, 1996, 3:00:00 AM10/30/96
to

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

Peter Moylan

unread,
Oct 31, 1996, 3:00:00 AM10/31/96
to

pete...@kuai.se wrote:

>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

Andre Doff

unread,
Oct 31, 1996, 3:00:00 AM10/31/96
to

In article <557fej$4...@kaon.kuai.se>, pete...@kuai.se wrote:

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

0 new messages