• ### Popular Now

• 13
• 18
• 19
• 27
• 10

#### Archived

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

# Proper way of Win32 Input

This topic is 5768 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

''Ello mates. What''s the proper way of getting keyboard input by the WndProc()? this is how I''m doing it:  bool bKeys[256]; ...WndProc()... case WM_KEYDOWN: bKeys[wParam] = true; return 0; case WM_KEYUP bKeys[wParam] = false return 0; ... ...  I use it this way: if(bKeys[VK_SOMETHING]) //Do Something The problem is that I''ve got a high repeat rate (I''ve got a screenshot function that''ll take 3 SShots with a ''tap'' on the F12) Have I missed something? If I use my Screenshot fuction this way:  ...WndProc().. case WM_KEYDOWN switch(wParam) { case VK_F12: Screenshot(); }  The repeat rate of the keyboard works perfectly (I need to press the F12 for about 1/2 seconds to keep pumping screenshots...) Someone?

##### Share on other sites
in the second code snipped you use the windows key autorepeat feature, in the first, you must implement
your own autorepeat code to get only one screenshot.

the following code should produce only one screenshot per keydown-event:

bool bKeys[256];
bool bTookScreenShot=false;

...WndProc()...
case WM_KEYDOWN:
bKeys[wParam] = true;
return 0;
case WM_KEYUP:
bKeys[wParam] = false;
if (wParam == VK_F12)
bTookScreenShot = false;
return 0;

...somewhere else...
if (bKeys[VK_F12] && !bTookScreenShot)
{
ScreenShot ();
bTookScreenShot = true;
}

This code does only produce 1 screenshot and there is no autorepeating.

##### Share on other sites
You could try this (just off the top of my head):

int iKeys[256];

case WM_KEYDOWN:
iKeys[wParam]++;
case WM_KEYUP:
iKeys[wParam] = 0;

that way, you can use it for stuff like what you need Screenshots (and it will only take 1 shot) i.e. (if (iKeys[VK_F12] == 1) { //screenshot } , and still use it for game related stuff like holding down an arrow key to move your space ship to the right.

That should work, but I''m running dangerously low on caffeine so you''ll have to try it yourself.

##### Share on other sites
hmm

I''m the previous A.P.

I went out earlier and something clicked in my mind about this problem, and the solution I gave was incomplete. I just can''t think what it was... so frustrating!

I''ll come back later if I figure it out.

##### Share on other sites
From MSDN:

quote:

Because of the autorepeat feature, more than one WM_KEYDOWN message may be posted before a WM_KEYUP message is posted. The previous key state (bit 30) can be used to determine whether the WM_KEYDOWN message indicates the first down transition or a repeated down transition.

so:

if (lParam & (1 << 30))
// key repeat
else
screenshot();

##### Share on other sites
That means you''ll have to use

if (lParam & (1 << 30))
// key repeat
else
// do something

for each different key checked, doesn''t it? I''m thinking...

case WM_KEYDOWN:
bKeys[wParam] = true;
bRepeat = (lParam & (1 << 30));
return 0;

case WM_KEYUP
bKeys[wParam] = false
return 0;

Then where you check bKeys[foo], just put this at the top, before anything''s done (you might have to change the value though, I dunno):

if(bRepeat) return 0;

I don''t know if that helps or hinders you, it''s merely my thoughts. Heh.

Chris ''coldacid'' Charabaruk <ccharabaruk@meldstar.com> <http://www.meldstar.com/ccharabaruk/>
Meldstar Studios <http://www.meldstar.com/> - Creation, cubed.

This message double ROT-13 encrypted for additional security.