This source file includes following definitions.
- SetForeground
- ForegroundHotKey
- OnHotKey
#include "ui/base/win/foreground_helper.h"
#include "base/logging.h"
#include "ui/gfx/win/window_impl.h"
namespace ui {
HRESULT ForegroundHelper::SetForeground(HWND window) {
DCHECK(::IsWindow(window));
ForegroundHelper foreground_helper;
return foreground_helper.ForegroundHotKey(window);
}
HRESULT ForegroundHelper::ForegroundHotKey(HWND window) {
set_window_style(WS_POPUP);
Init(NULL, gfx::Rect());
static const int kHotKeyId = 0x0000baba;
static const int kHotKeyWaitTimeout = 2000;
window_ = window;
RegisterHotKey(hwnd(), kHotKeyId, 0, VK_F22);
MSG msg = {0};
PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE);
INPUT hotkey = {0};
hotkey.type = INPUT_KEYBOARD;
hotkey.ki.wVk = VK_F22;
if (1 != SendInput(1, &hotkey, sizeof(hotkey))) {
LOG(WARNING) << "Failed to send input";
return E_FAIL;
}
SetTimer(hwnd(), kHotKeyId, kHotKeyWaitTimeout, NULL);
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
if (WM_HOTKEY == msg.message)
break;
if (WM_TIMER == msg.message) {
SetForegroundWindow(window);
break;
}
}
UnregisterHotKey(hwnd(), kHotKeyId);
KillTimer(hwnd(), kHotKeyId);
DestroyWindow(hwnd());
return S_OK;
}
void ForegroundHelper::OnHotKey(int id, UINT vcode, UINT modifiers) {
SetForegroundWindow(window_);
}
}