delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/10/17/05:49:09

From: mert0407 AT sable DOT ox DOT ac DOT uk (George Foot)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: problem with readkey()
Date: 16 Oct 1997 20:34:05 GMT
Organization: Oxford University, England
Lines: 43
Message-ID: <625tnt$1rj$1@news.ox.ac.uk>
References: <01bcda6c$d9e94d80$0200a8c0 AT ingo>
NNTP-Posting-Host: sable.ox.ac.uk
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

On 16 Oct 1997 19:21:35 GMT in comp.os.msdos.djgpp Ingo Ruhnke
(ruhnke AT owl-online DOT de) wrote: 

: I got here some simple code which dosen't run as I expect it to work:

: void main()
  ^^^^
This is incorrect; the main function *must* return an `int'. But that's not
why your program didn't work...

:  if (readkey() == 'a') {

didn't work and:

:  if ((c = readkey()) == 'a') {

did work, where c is a char.

The reason for this is that the readkey function returns an int. The
composition of this int is explained clearly in the Allegro docs (which I
don't have here right now); IIRC the lower 8 bits are the ASCII code, the
next 8 are the keyboard scan code. Alt-key just gives the scan code, with the
ASCII code equal to zero.

So, if you want to find out what ASCII code was pressed you need to extract
the lower 8 bits. Your second example, assigning the return value to a char,
achieved this. To do so without needing an intermediate variable, try:

if ((readkey()&0xff) == 'a') { ...

Also note that this is only checking for a lower-case `a', without CTRL or
ALT pressed. More generally:

if ((readkey()>>8) == KEY_A) { ...

will pick up anything involving the A key (by checking the scancode rather
than the ASCII code).

For several examples of using readkey, and a fuller (correct) description of
what it returns, look it up in the Allegro documentation. 

-- 
george.foot at merton.oxford.ac.uk

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019