Waitkey() will call keyhit and return the code of the keyhit. At the same time the key is added to a queue for use by Getchar. When Getchar runs it checks this queues and finds the key even though it has been returned by waitkey.
If you use Waitchar, rather than waitkey, you'll see the correct results.
If you use Waitchar, rather than waitkey, you'll see the correct results.