Allow mouse clicks only when focused.

Fixes #6486.
This commit is contained in:
Unknown W. Brackets 2015-02-28 16:01:41 -08:00
parent cf88c7ff67
commit 7aa5914a7a
3 changed files with 39 additions and 4 deletions

View File

@ -58,6 +58,8 @@ namespace WindowsRawInput {
static void *rawInputBuffer;
static size_t rawInputBufferSize;
static bool menuActive;
static bool focused = true;
static bool mouseRightDown = false;
void Init() {
RAWINPUTDEVICE dev[3];
@ -171,7 +173,18 @@ namespace WindowsRawInput {
return 0;
}
void ProcessMouse(RAWINPUT *raw, bool foreground) {
static bool MouseInWindow(HWND hWnd) {
POINT pt;
if (GetCursorPos(&pt) != 0) {
RECT rt;
if (GetWindowRect(hWnd, &rt) != 0) {
return PtInRect(&rt, pt) != 0;
}
}
return true;
}
void ProcessMouse(HWND hWnd, RAWINPUT *raw, bool foreground) {
if (menuActive && UpdateMenuActive()) {
// Ignore mouse input while a menu is active, it's probably interacting with the menu.
return;
@ -193,12 +206,27 @@ namespace WindowsRawInput {
key.flags = KEY_DOWN;
key.keyCode = windowsTransTable[VK_RBUTTON];
NativeTouch(touch);
if (MouseInWindow(hWnd)) {
NativeKey(key);
}
mouseRightDown = true;
} else if (raw->data.mouse.usButtonFlags & RI_MOUSE_RIGHT_BUTTON_UP) {
key.flags = KEY_UP;
key.keyCode = windowsTransTable[VK_RBUTTON];
NativeTouch(touch);
if (MouseInWindow(hWnd)) {
if (!mouseRightDown) {
// This means they were focused outside, and right clicked inside.
// Seems intentional, so send a down first.
key.flags = KEY_DOWN;
NativeKey(key);
key.flags = KEY_UP;
NativeKey(key);
} else {
NativeKey(key);
}
}
mouseRightDown = false;
}
// TODO : Smooth and translate to an axis every frame.
@ -229,7 +257,7 @@ namespace WindowsRawInput {
break;
case RIM_TYPEMOUSE:
ProcessMouse(raw, foreground);
ProcessMouse(hWnd, raw, foreground);
break;
case RIM_TYPEHID:
@ -241,6 +269,10 @@ namespace WindowsRawInput {
return DefWindowProc(hWnd, WM_INPUT, wParam, lParam);
}
void GainFocus() {
focused = true;
}
void LoseFocus() {
// Force-release all held keys on the keyboard to prevent annoying stray inputs.
KeyInput key;
@ -250,6 +282,7 @@ namespace WindowsRawInput {
key.keyCode = *i;
NativeKey(key);
}
focused = false;
}
void NotifyMenu() {

View File

@ -22,6 +22,7 @@ namespace WindowsRawInput {
LRESULT Process(HWND hWnd, WPARAM wParam, LPARAM lParam);
// Not actually RawInput but it kinda belongs here anyway.
LRESULT ProcessChar(HWND hWnd, WPARAM wParam, LPARAM lParam);
void GainFocus();
void LoseFocus();
void NotifyMenu();
void Shutdown();

View File

@ -1083,6 +1083,7 @@ namespace MainWindow
{
bool pause = true;
if (wParam == WA_ACTIVE || wParam == WA_CLICKACTIVE) {
WindowsRawInput::GainFocus();
InputDevice::GainFocus();
g_activeWindow = WINDOW_MAINWINDOW;
pause = false;