Release held keys on the keyboard when window focus is lost.

This commit is contained in:
The Dax 2014-01-02 23:31:15 -05:00
parent c51b117ba2
commit f90aebcebf

View File

@ -103,6 +103,7 @@ static RECT g_normalRC = {0};
static std::wstring windowTitle; static std::wstring windowTitle;
extern bool g_TakeScreenshot; extern bool g_TakeScreenshot;
extern InputState input_state; extern InputState input_state;
static std::vector<int> keyboardKeysDown;
#define TIMER_CURSORUPDATE 1 #define TIMER_CURSORUPDATE 1
#define TIMER_CURSORMOVEUPDATE 2 #define TIMER_CURSORMOVEUPDATE 2
@ -1083,13 +1084,15 @@ namespace MainWindow
} }
if (wParam == WA_INACTIVE) { if (wParam == WA_INACTIVE) {
// Force-release TAB, which is the most annoying one when alt tabbing // Force-release all held keys on the keyboard to prevent annoying stray inputs.
// This isn't exactly a correct solution but will fix this annoyance for many.
KeyInput key; KeyInput key;
key.deviceId = DEVICE_ID_KEYBOARD; key.deviceId = DEVICE_ID_KEYBOARD;
key.keyCode = NKCODE_TAB;
key.flags = KEY_UP; key.flags = KEY_UP;
NativeKey(key);
for (auto i = keyboardKeysDown.begin(); i != keyboardKeysDown.end(); ++i) {
key.keyCode = *i;
NativeKey(key);
}
} }
} }
break; break;
@ -1546,12 +1549,20 @@ namespace MainWindow
key.keyCode = windowsTransTable[GetTrueVKey(raw->data.keyboard)]; key.keyCode = windowsTransTable[GetTrueVKey(raw->data.keyboard)];
if (key.keyCode) { if (key.keyCode) {
NativeKey(key); NativeKey(key);
const bool keyNotHeld = std::find(keyboardKeysDown.begin(), keyboardKeysDown.end(), key.keyCode) == keyboardKeysDown.end();
if (keyNotHeld)
keyboardKeysDown.push_back(key.keyCode);
} }
} else if (raw->data.keyboard.Message == WM_KEYUP) { } else if (raw->data.keyboard.Message == WM_KEYUP) {
key.flags = KEY_UP; key.flags = KEY_UP;
key.keyCode = windowsTransTable[GetTrueVKey(raw->data.keyboard)]; key.keyCode = windowsTransTable[GetTrueVKey(raw->data.keyboard)];
if (key.keyCode) { if (key.keyCode) {
NativeKey(key); NativeKey(key);
auto keyHeld = std::find(keyboardKeysDown.begin(), keyboardKeysDown.end(), key.keyCode);
if (keyHeld != keyboardKeysDown.end())
keyboardKeysDown.erase(keyHeld);
} }
} }
} else if (raw->header.dwType == RIM_TYPEMOUSE) { } else if (raw->header.dwType == RIM_TYPEMOUSE) {