1

I am trying to do a very simple task - have an event occur when a key is pressed - but am having a lot of difficulty implementing it.

I am using the Win32 API. I have been asked what framework I am using but I don't know that. I am using Visual C++ and the program is a Windows program.

All I want to do is have an event occur if a specific key is pressed. For this example I am using the 's' key, and the event is an integer either being set to 1 or 0; whichever it wasn't set to at the time of the key press (I would use bool but I don't know how it works just yet).

I have been told to use GetKeyState(), and then told that this is actually no good. I have also been told to use WM_KEYDOWN but can't work out how this works... Surely what I am doing must be absolute basic programming (keyboard input > output) but I can't get a clear explanation as to how it works?!

I have tried using the following, with no luck:

int Flag;
if (GetKeyState(115) == 1 && Flag == 0) Flag = 1;
if (GetKeyState(115) == 1 && Flag == 1) Flag = 0;

I have also tried using this:

if (GetKeyState(115) & 0x8000 && Flag == 0) Flag = 1;
if (GetKeyState(115) & 0x8000 && Flag == 1) Flag = 0;

Neither work. Does anyone know how I could implement WM_KEYDOWN?

I am using a Windows Message Loop

CaptainProg
  • 5,610
  • 23
  • 71
  • 116
  • 2
    are you using a Windows Message loop (http://en.wikipedia.org/wiki/Message_loop_in_Microsoft_Windows) ? – SirDarius Jun 13 '11 at 15:19
  • Please read Petzold's book, Programming Windows. You can't learn Windows GUI programming overnight. It's not trivial. You need to walk before you can run. – David Heffernan Jun 13 '11 at 15:21
  • @SirDarius Yes, I think I am. My WinMain function starts with (HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nFunsterStil). Is that what you mean? – CaptainProg Jun 13 '11 at 15:21
  • Is it MFC or Plain Win32? If MFC, you can handle using message map. Otherwise you need to look into Window Proc –  Jun 13 '11 at 15:22
  • You are also making it needlessly hard for yourself writing against raw Win32 API. Do yourself a favour and start with a higher level framework, e.g. WinForms or VCL. – David Heffernan Jun 13 '11 at 15:22
  • @David I need to work low-level as my program is a psychophysical experiment and needs to be nanosecond perfect. I understand that I am starting at the wrong end of programming and appreciate that - and in my spare time I am working from the other end. – CaptainProg Jun 13 '11 at 15:25
  • You need to create and display a window, registered as a specific window class. Then you need to create a Window Procedure that will be called via DispatchMessage... and I'm not sure using the Win32 api instead of a higher level API guarantees you nanosecond perfection in any way :) – SirDarius Jun 13 '11 at 15:27
  • @SirDarius Thanks, I already have the window created and working fine. In fact most of my program is complete, this is just a final extra feature I'd like to add. The question is how to get WM_KEYDOWN working? Everything else in the program is controlled by mouse, dialog boxes, and an eye movement tracker – CaptainProg Jun 13 '11 at 15:29
  • @Captain Don't kid yourself that using low level API will be nanosecond perfect. WinForms/VCL are wrappers around Win32 and I would expect them all to be equally incapable of delivering timings to nanosecond accuracy. Really, if you want my advice, start higher up. – David Heffernan Jun 13 '11 at 15:30
  • @David Thanks, it's noted for the future. In this case however my program is all but finished - a rewrite could take months. All I want is to have a keypress toggle a bool. I didn't realise that would be so difficult! – CaptainProg Jun 13 '11 at 15:32
  • 2
    @CaptainProg when the button is pressed is there a window/dialog up? Was it created with DialogBox or CreateWindow? – MerickOWA Jun 13 '11 at 15:32
  • @MerickOWA There is a Window up but no dialog at the time – CaptainProg Jun 13 '11 at 15:34
  • Well the question is unclear. What do you mean by "Does anyone know how I could implement `WM_KEYDOWN`?" You have said that you are "using a Windows Message Loop;" why can you not handle the message there? That's what the message loop is for. Are you not getting the message? (If so, what does Spy++ tell you; is the message actually being posted to your window?) – James McNellis Jun 13 '11 at 15:50

1 Answers1

6

There are several ways to solve this problem. None of which will give you "nano-second" accuracy but here they are.

If you want the keypress to be recieved by an active window or dialog you handle a WM_KEYDOWN even in the WINPROC of the dialog/window like so.

void InSomePlace()
{
  WNDCLASS wndClass
  ZeroMemory( &wndClass, sizeof(wndClass) );

  // Initialize wndClass members here
  wndClass.lpszClassName = _T("MyWindow");
  wndClass.lpfnWndProc = &MyWndProcHandler; // 

  RegisterClass( &wndClass );
  HWND hWnd = CreateWindow( _T("MyWindow", /* lots of other parameters */ );

  MSG msg;
  BOOL bRet;
  while ( (bRet = GetMessage( &msg, hWnd, 0, 0 )) != 0 )
  {
    if (bRet == -1)
    {
      // handle the error and possibly exit
    }
    else
    {
      TranslateMessage(&msg); 
      DispatchMessage(&msg); 
    }
  }
}

LRESULT CALLBACK MyWndProcHandler( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
  switch ( uMsg )
  {
    // Lots of case statements, in particular you want a WM_KEYDOWN case
    case WM_KEYDOWN:
      if ( wParam == 'S' )
      {
        // Do something here
        return 0L;
      }
      break;
  }

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

For a DialogBox its very similar, you would still have a DLGPROC which is passed as the last parameter to DialogBox/CreateDialog

void InSomePlace( HINSTANCE hInstance, HWND hParentWindow )
{
  DialogBox( hInstance, _T("MyDialogTemplate"), hParentWindow, &MyDialogProc );
}

INT_PTR CALLBACK MyDialogProc( HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
  case ( uMsg )
  {
    // Lots of case statements, in particular you want a WM_KEYDOWN case
    case WM_KEYDOWN:
      if ( wParam == 'S' )
      {
        // Do something here
        SetWindowLong(hwndDlg, DWL_MSGRESULT, 0L);
        return TRUE;
      }
      break;
  }
  return FALSE;
}
MerickOWA
  • 7,453
  • 1
  • 35
  • 56