WinAPI keyboard focus

Hi, guys. I'm making a very simple level editor for my game and I've got a problem. How can I set keyboard focus to one window and do not lose it when I click the other controls? For example I need to move camera with arrow keys, but what I've got is moving selection around other controls(checkboxes, buttons, etc.) and losing input from window, which needs to hold keyboard focus! I've googled for hours already and haven't found the correct solution. Please, help :)
Have you tried handling WM_MOUSEACTIVATE and returning MA_NOACTIVATE ??

See MSDN entry on WM_MOUSEACTIVATE for more details.


This is what I have:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
BOOL MainDialog_OnCommand(HWND hWnd, WORD wCommand, WORD wNotify, HWND hControl)
{	

	switch (wCommand)
	{
	//...
	}
return TRUE;
}

BOOL MainDialogProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	
	switch (uMsg)
	{
		
	case WM_INITDIALOG:
		//...
		break; 
	case WM_COMMAND:
		return MainDialog_OnCommand(hWnd, LOWORD(wParam),
			HIWORD(wParam), (HWND)lParam);
        case WM_MOUSEACTIVATE:
		if( (HWND)wParam != hWndVP) //hWndVP is hwnd of window I want to get focus
			return MA_NOACTIVATE;
		else
		{
			SetFocus(hWndVP);
			return MA_ACTIVATE;
		}
		break;
	case WM_PAINT:
			return MainDialog_OnCommand(hWnd, LOWORD(wParam),
			HIWORD(wParam), (HWND)lParam);
		break;
	case WM_CLOSE:
		EndDialog(hWnd, 0);
		return TRUE;
	}

	return FALSE;

}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int) 
{
	DialogBoxParam(hInstance, MAKEINTRESOURCE(100),  //100 - ID of main window
		NULL, (DLGPROC)MainDialogProc, 0);
	return 0;
}

(I've deleted some code, which is not important here)
It doesn't work, I'm probably wrong somewhere... I tried WM_KEYDOWN in MainDialogProc but it doesn't work, because window keeps losing its focus.

UPD:Problem solved, mb someone will find this in future and it'll help them.

In DialogProc

1
2
3
4
5
6
7
8
if(GetAsyncKeyState(VK_LEFT))
	{
		
			SetFocus(hWndVP);
			//actions here
		}
		return true;
	}

This will disable pre-defined button(VK_LEFT here) for other windows. :)
Last edited on
Topic archived. No new replies allowed.