Jump to content

[SOLVED] Callback when application is focused again


photo

Recommended Posts

Hi,

I am currently trying to find a way to get an notification, when the UNIGINE application window will get back in focus by the user? With setBackgroundMode() and isActive() there is a way of doing that but in my opinion it is not a satisfying way.

We are currently developing some kind of in-app editor and the application should check, when it gets into focus again, if some external files are modified or not (like the real editor). But when loosing focus we also want to pause the UNIGINE app completely, so setBackgroundMode to zero will be the only thing to do that.

So is there a way to retreive a callback when the UNIGINE app loses focus and get back again?

Best

Christian

Link to comment

Hello,

For Windows, you can try using hooks. With this approach, you will have access to all window events. Below is an example for focus events:

#include <Windows.h>

HHOOK hhook{nullptr};
HWND main_hwnd{nullptr};

LRESULT CallWndProc(int nCode, WPARAM wParam, LPARAM lParam)
{
	if (nCode < 0)
		return CallNextHookEx(hhook, nCode, wParam, lParam);

	CWPSTRUCT *cwp = (CWPSTRUCT *)(lParam);
	HWND hwnd = cwp->hwnd;
	UINT message = cwp->message;

	if (hwnd == main_hwnd)
	{
		switch (message)
		{
			case WM_SETFOCUS:  Unigine::Log::message("set focus\n");  break;
			case WM_KILLFOCUS: Unigine::Log::message("kill focus\n"); break;
			default: break;
		}
	}

	return CallNextHookEx(hhook, nCode, wParam, lParam);
}

int AppSystemLogic::init()
{
	hhook = SetWindowsHookEx(WH_CALLWNDPROC, CallWndProc, (HINSTANCE)NULL, GetCurrentThreadId());
	main_hwnd = (HWND)App::getHandle();

	return 1;
}

int AppSystemLogic::shutdown()
{
	UnhookWindowsHookEx(hhook);
	return 1;
}

 

  • Like 2
  • Thanks 1
Link to comment
  • silent changed the title to [SOLVED] Callback when application is focused again
×
×
  • Create New...