Ok, here goes, I've got a staticwindow where I am putting the information from keyboard presses and onscreen button presses. When I run the program, I can press the keys and they show up just fine. I then press the button on the screen with my mouse which also works. However, now when I try to press a key on the board again, the WM_CHAR case is no longer getting information from the wParam.

I have found that by minimizing the window, or placing a messagebox inside of the WM_COMMAND case, it works properly. I've looked extensively and haven't found a proper solution.

LRESULT CALLBACK WinProc(HWND hwnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
	
	LPTSTR pushed_button = new TCHAR[10];
	LPTSTR current_string = new TCHAR[1010];
	GetWindowText(hwndStatic, current_string, 1000);
	switch(Msg){ 
		case WM_COMMAND: 
		//Enable the following message box, or minimize and restore the window for functionality to return.
		//MessageBox(hwnd,L"button press",L"Item details",0);
		switch(LOWORD(wParam)){ //the ID is is wParam        
			case 20:
				GetWindowText(button[0], pushed_button,10);
				lstrcat(current_string,pushed_button);
				SetWindowText(hwndStatic, current_string);
			break;
			case 21:
				GetWindowText(button[1], pushed_button,10);
				lstrcat(current_string,pushed_button);
				SetWindowText(hwndStatic, current_string );
				
			break;
		}
		return DefWindowProc(hwnd, Msg, wParam, lParam);
	break;
	case WM_CHAR: //the case that stops working.
		switch(wParam){
		
			case 'A': 
				lstrcat(current_string,L"A");
				SetWindowText(hwndStatic, current_string );
			break;

Any help is appreciated. Thank-you

Recommended Answers

All 3 Replies

When you click a button, the focus will change to the button (the button will have keyboard focus). Therefore, any keypresses will be sent to the window procedure of the button, which is in user32.dll I believe. So, you might try using a SetFocus() call back to your main window within the button press event handler so the keystrokes will be sent there instead. I see you are a new poster. If someone solves your problem in this forum, its considered polite to indicate such by marking it solved.

Thank-you for the info. The setfocus works wonderfully. I had figured that it was losing focus, but could not find how to get it back. Thanks for the tip on setting the post as solved, will be doing that!

Focus can be a tricky thing in Windows - as you found out. As I mentioned, your WndProc() wasn't getting the keyboard activity, the WndProc() of the button control was. However, its possible to 'hook' the button WndProc() using the SetWindowLong() function, and in that manner receive the keypresses first in your app. If interested in that look up SetWindowLong() with GWL_WNDPROC parameter. Its called subclassing.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.