Sign in to follow this  
Rhaal

Problems with GetAsyncKeyState

Recommended Posts

Ok I've decided to try and use windows for my input. If this fails, I'm going to resort to SDL for input since I'm comfortable with it. I basically use the gamestate stack system with each state calling an input function to receive input. The problem is my program is not exiting when I press the UP or DOWN arrow key. When I click the left mouse button, however, it works. main.cpp
// ********** SYSTEM INCLUDES **********
#include <stack>
#include <iostream>
// ********** USER INCLUDES ************
#include "EventReceiver.h"
// ********** NAMESPACES ***************
using namespace std;


// Game state stack.
struct gStateStruct { void (*StatePointer)(); };
stack<gStateStruct> gStateStack;

// Event handler
LoriumEventReceiver* EventReceiver;

// Function: void Input()
void Input()
{
  /*ESC*/// Quit
  /*if(EventReceiver->KeyDown( static_cast<const int>(1B) ) )//ESC
  {
    // Pop one state off of the stack
    gStateStack.pop();
    return;
  }*/
	if( EventReceiver->MouseLeftKeyDown())
  {
    gStateStack.pop();
    return;
	}
	if(EventReceiver->KeyDown(28)) // DOWN
  {
    gStateStack.pop();
    return;
	}
	if(EventReceiver->KeyDown(25)) // LEFT
	{
    return;
	}
	if(EventReceiver->KeyDown(26)) // RIGHT
	{
    return;
	}
}

//
// GAMESTATE: gsTitleScreen
//
void gsTitleScreen()
{
  // Start the first song.
  // Handle input.
  Input();
}

// Function: void Init()
void Init()
{
  EventReceiver = new LoriumEventReceiver;

  // WTF test
  std::cout << "Init Done" << std::endl;

  // Initialize the stack with the TitleScreen.
  gStateStruct state;
  state.StatePointer = gsTitleScreen;
  gStateStack.push(state);
}

// Function: void Shutdown()
//    Shuts everything down.
void Shutdown()
{
  delete EventReceiver;
}

// w00t!
int main(int argc, char **argv)
{
  Init();

  // Game loop.
  while(!gStateStack.empty())
  {
    gStateStack.top().StatePointer();
  }

  Shutdown();

  return 0;
}
EventReceiver.h
#ifndef LX_EVENTRECEIVER_H
#define LX_EVENTRECEIVER_H
#include <irrlicht.h>
#include <windows.h>

class LoriumEventReceiver
{
public:
  LoriumEventReceiver(){}
  ~LoriumEventReceiver(){}
    
  virtual BOOL KeyDown(DWORD vk_code)
  {
    return (GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0;
  }

  virtual BOOL KeyUp(DWORD vk_code)
  {
    return (GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1;
  }

  virtual BOOL MouseLeftKeyDown()
  {
    return (GetAsyncKeyState(VK_LBUTTON) & 0x8000) ? 1 : 0;
  }

  virtual BOOL MouseLeftKeyUp()
  {
    return (GetAsyncKeyState(VK_LBUTTON) & 0x8000) ? 0 : 1;
  }

  virtual BOOL MouseRightKeyDown()
  {
    return (GetAsyncKeyState(VK_RBUTTON) & 0x8000) ? 1 : 0;
  }

  virtual BOOL MouseRightKeyUp()
  {
    return (GetAsyncKeyState(VK_RBUTTON) & 0x8000) ? 0 : 1;
  }
};

#endif

Share this post


Link to post
Share on other sites
I understand it looks like I just dumped a bunch of code on everyone, but here's where the problem lies (in my Input() function):

if( EventReceiver->MouseLeftKeyDown())
{
gStateStack.pop();
return;
}
if(EventReceiver->KeyDown(28)) // DOWN
{
gStateStack.pop();
return;
}


The mouse works, but the keys dont register.

Share this post


Link to post
Share on other sites
Don't quote me on this, but I'm pretty sure the AsyncKeyState call is -really- Async. If the key is down and up again by the time the call is made, it will 'miss' the keystroke. See if it will exit when you hold the down key down.

[imo] windows messages are far better, far more reliable, far more straightfoward than the LaMothian GetAsyncKeyState.

Share this post


Link to post
Share on other sites
Quote:
Original post by Telastyn
Don't quote me on this

[lol] [lol]

I think I'll try the standard WM messages. But since I'm making a console app, how do I tell Windows to use my callback function?

Share this post


Link to post
Share on other sites
Ah, good catch prototype. That should make it work.

For console stuff, just standard cin/read/ReadConsole/getch stuff [combined with kbhit for windows or select for unixy machines if non-blocking reads are needed]. Though it's a little less clear there.

Share this post


Link to post
Share on other sites
I could swear I tried VK_*, but it didn't work. I just tried it, and it worked so I must have been missing windows.h or something else was going on last time it didn't work.

You could see from my "ESCAPE" handling that my next question was going to be why in the heck doesn't 1B work. Well, now it makes sense.

I'm also going to try what you said, Telastyn.

Thanks again guys! I stepped away for a few months and the basics have come back to bite me.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this