• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# hold back key

## Recommended Posts

I don''t know how kan I make my heros move light. I just want to know how can I understand if key is press. Not to write from Keyboard buffer and wait 1/4 sec after first read from buffer. QBASIC, PASCAL, C, C++, VB, PERL,

##### Share on other sites
In C++, define these two macros at the top of the file:

#define KEYDOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 :0)
#define KEYUP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)

IMPORTANT- make sure that each macro fits onto 1 line.

Also make sure that you include:
#include <windows.h>
#include <windowsx.h>
#include <mmsystem.h>
#include <stdio.h>// i'm not sure if you actually need
#include <stdlib.h>// these last 3
#include <math.h>

then you can test for certain specific keys,
such as:
if (KEYDOWN(VK_UP )) {
//move character up
}
if (KEYUP(VK_UP)) {
// user released the up key
}
This only works for keys such as VK_SPACE, VK_RETURN, VK_F1, VK_UP, VK_DOWN, etc. (special keys). It doesnt work for regular keys like "a" or "b" I'm not actually sure how to do that. If anyone knows I would appreciate that.

Hope this helps

Dan

Edited by - GameDev135 on August 14, 2001 12:30:54 PM

##### Share on other sites
To check for keys like A and B, just use

  if ( KEYDOWN( ''A'' ) ) // Do stuff

easy eh

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627662
• Total Posts
2978506

• 9
• 10
• 12
• 22
• 13