Sign in to follow this  

WM_KEYDOWN not working

This topic is 3844 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi, can someone tell me why keydown msg is not received when i press a key this is my code
case WM_KEYDOWN:         
		{   
		    if(VK_SPACE == wparam)
		       MessageBox(NULL,"Hi", "hi", MB_OK);
                    
                       MessageBox(NULL,"Hi", "hi", MB_OK);
		} break;    
As I understand this is supposed to display an msgbox if i press any key, but nothing happens [sad] Help appreciated, thanks [smile]

Share this post


Link to post
Share on other sites
You should probably not be checking upon WM_KEYDOWN. It's slow, and it allows repeat. Fortunately, you're using a MessageBox, which is modal, and won't allow anything else to happen.

Also, it won't work with System keys, and will only work in the window that has keyboard focus. I'm guessing you're probably trying to check for the keydown in a window that does not have the keyboard focus.

[Edited by - Nytegard on June 8, 2007 4:54:07 PM]

Share this post


Link to post
Share on other sites
works perfectly fine for me (check my WM_KEYDOWN, looks same as yours). something else must be wrong with your code. missing or misplace break statement maybe? would need to see full code.


#include<windows.h>

static LPCTSTR title = TEXT("Fundamental Window");
static LPCTSTR cname = TEXT("Fundamental Window");
static HINSTANCE basead = 0;

LRESULT CALLBACK MainWndProc(HWND window, UINT message, WPARAM wparam, LPARAM lparam);

int WINAPI WinMain(HINSTANCE instance, HINSTANCE, LPSTR, int)
{
basead = instance;

WNDCLASS wc;
wc.style = CS_DBLCLKS;
wc.lpfnWndProc = MainWndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = basead;
wc.hIcon = 0;
wc.hCursor = LoadCursor(0, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = 0;
wc.lpszClassName = title;

if(!RegisterClass(&wc))
return MessageBox(0, TEXT("Could not register main window."), TEXT("Error"), MB_ICONSTOP);

DWORD style = WS_OVERLAPPEDWINDOW;
int x = CW_USEDEFAULT; int w = CW_USEDEFAULT;
int y = CW_USEDEFAULT; int h = CW_USEDEFAULT;

HWND window = CreateWindowEx(0, cname, title, style, x, y, w, h, 0, 0, basead, 0);
if(!window) return MessageBox(0, TEXT("Could not create main window."), TEXT("Error"), MB_ICONSTOP);

ShowWindow(window, SW_SHOW);
UpdateWindow(window);

MSG msg;
while(GetMessage(&msg, 0, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}

return (int)msg.wParam;
}

LRESULT CALLBACK MainWndProc(HWND window, UINT message, WPARAM wparam, LPARAM lparam)
{
switch(message) {
case(WM_KEYDOWN) : {
if(VK_SPACE == wparam)
MessageBox(NULL, TEXT("Hi"), TEXT("hi"), MB_OK);
MessageBox(NULL,TEXT("Hi"), TEXT("hi"), MB_OK);
break;
}
case(WM_DESTROY) : {
PostQuitMessage(0);
break;
}
}
return DefWindowProc(window, message, wparam, lparam);
}

Share this post


Link to post
Share on other sites

This topic is 3844 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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