How can my window without focus get key presses?
Printable View
How can my window without focus get key presses?
SetFocus(hWnd)
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.
SetWindowsHookEx() passing WH_KEYBOARD as the hook ID. Read the MSDN documentation for the function for more information.Quote:
How can my window without focus get key presses?
oops, sorry, I think my ADD kicked in there, I completely misread his post.
How can I use the hook handle to get the key presses then? I don't know much about hooks...
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.
Mixed up something awful...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);
}
Ofcourse this doesn't work...
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.