Thread: How can my window without focus get key presses?

  1. #1
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318

    How can my window without focus get key presses?

    How can my window without focus get key presses?

  2. #2
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    SetFocus(hWnd)

  3. #3
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    rockytriton, that sets focus to his window, it doesn't allow his window to pick up key presses. I would suggest putting your program in a loop, using GetAsyncKeyState() to check to see if your wanted key was last pressed or is being pressed.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    How can my window without focus get key presses?
    SetWindowsHookEx() passing WH_KEYBOARD as the hook ID. Read the MSDN documentation for the function for more information.

  5. #5
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    oops, sorry, I think my ADD kicked in there, I completely misread his post.

  6. #6
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    How can I use the hook handle to get the key presses then? I don't know much about hooks...

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Did you read the MSDN documentation? At the end of the remarks section, it gives an example on how to install and release hook procedures.

  8. #8
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Code:
    #include <windows.h>
    typedef struct _MYHOOKDATA 
    { 
        int nType; 
        HOOKPROC hkprc; 
        HHOOK hhook; 
    } MYHOOKDATA;
    int index;
    MYHOOKDATA myhookdata;
    MSG messages;
    LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam);
    int WINAPI WinMain(HINSTANCE hThisInstance,HINSTANCE hPrevInstance,LPSTR lpszArgument,int nFunsterStil){
        myhookdata.nType = WH_KEYBOARD; 
        myhookdata.hkprc = KeyboardProc;
            myhookdata.hhook = SetWindowsHookEx( 
            myhookdata.nType, 
            myhookdata.hkprc, 
            (HINSTANCE) NULL, GetCurrentThreadId());
        while (GetMessage (&messages, NULL, 0, 0)){
            TranslateMessage(&messages);
            DispatchMessage(&messages);
        }
        return 0;
    }
    LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) 
    { 
        CHAR szBuf[128]; 
        HDC hdc; 
        static int c = 0; 
        size_t cch; 
    	size_t * pcch;
    	HRESULT hResult;
     
        if (nCode < 0)  // do not process message 
            return CallNextHookEx(myhookdata.hhook, nCode, 
                wParam, lParam); 
        MessageBox(HWND_DESKTOP,"hello","hello",MB_OK);
        return CallNextHookEx(myhookdata.hhook, nCode, wParam, 
            lParam); 
    }
    Mixed up something awful...
    Ofcourse this doesn't work...
    Last edited by maxorator; 11-09-2005 at 01:25 PM.

  9. #9
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    First of all, your hook should be set from a DLL file...

    Do a search on this board, I posted a complete write-up on how to do this somewhere.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Just starting Windows Programming, School me!
    By Shamino in forum Windows Programming
    Replies: 17
    Last Post: 02-22-2008, 08:14 AM
  2. Adding colour & bmps to a Win 32 Window??
    By carey_sizer in forum Windows Programming
    Replies: 4
    Last Post: 09-04-2004, 05:55 PM
  3. problem with open gl engine.
    By gell10 in forum Game Programming
    Replies: 1
    Last Post: 08-21-2003, 04:10 AM
  4. Directional Keys - Useing in Console
    By RoD in forum C++ Programming
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM
  5. OpenGL and Windows
    By sean345 in forum Game Programming
    Replies: 5
    Last Post: 06-24-2002, 10:14 PM