4 Replies - 27377 Views - Last Post: 20 July 2010 - 08:52 AM Rate Topic: -----

#1 Guest_lochness*


Reputation:

detecting when a key is pressed!

Posted 19 July 2010 - 06:56 PM

anyone know what function i can use from the windows api to tell if a key on the keyboard has been pressed :/? im pretty much wanting to count how many keys are pressed in a minute?? -thx
Is This A Good Question/Topic? 0

Replies To: detecting when a key is pressed!

#2 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3213
  • View blog
  • Posts: 19,241
  • Joined: 14-September 07

Re: detecting when a key is pressed!

Posted 19 July 2010 - 07:00 PM

Check out the family of keyboard input functions. What you need will depend on whether or not you want to check specific keys, any key presses, etc...
Was This Post Helpful? 0
  • +
  • -

#3 Guest_lochness*


Reputation:

Re: detecting when a key is pressed!

Posted 19 July 2010 - 07:10 PM

im assuming i would use WM_KEYDOWN and WM_KEYUP but still unsure of how to use this? it's not a function so im a little lost!
Was This Post Helpful? 0

#4 snoopy11   User is offline

  • Engineering ● Software
  • member icon

Reputation: 1594
  • View blog
  • Posts: 5,010
  • Joined: 20-March 10

Re: detecting when a key is pressed!

Posted 20 July 2010 - 03:16 AM

LRESULT CALLBACK WindowProc
(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{

//window stuff such as buttons

switch (message)
{
case: WM_KEYDOWN
// do something

case: WM_KEYUP
// do something 

}


return DefWindowProc (hwnd,message,wParam,lParam);
}



Im assumming its a Win32 project your working on you dont say in your post ??
You dont say much in your post mind you ??
Was This Post Helpful? 1
  • +
  • -

#5 Aphex19   User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 619
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: detecting when a key is pressed!

Posted 20 July 2010 - 08:52 AM

You could use GetAsyncKeyState(), but this is not always the best solution. If you are using the windows API, a good method would be to have a global array of virtual keys, for example.

bool keys[256];


Then when you detect a key press you can set the correct state of the key that is pressed, for example.

switch (message)
{
    case: WM_KEYDOWN
        keys[wParam] = true; // wParam holds the key pressed/released
        break;
 
    case: WM_KEYUP
        keys[wParam] = false;
        break;	 
}


Then you can simply check for a key press like so.

if (keys[keyCode]) 
{
    // do something
    keys[keyCode] = false; // prevent from repeating the detection of the key 
}

This post has been edited by Aphex19: 20 July 2010 - 08:54 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1