From a972b5d0dc7c06eb57b19a56187fcbb1561a592d Mon Sep 17 00:00:00 2001 From: Kingcom Date: Tue, 5 Nov 2013 11:29:55 +0100 Subject: [PATCH 1/2] Correct use of GetAsyncKeyState --- Windows/Debugger/CtrlDisAsmView.cpp | 20 ++++++++++---------- Windows/Debugger/CtrlMemView.cpp | 8 ++++---- Windows/GEDebugger/CtrlDisplayListView.cpp | 14 +++++++------- Windows/W32Util/Misc.h | 8 ++++++++ 4 files changed, 29 insertions(+), 21 deletions(-) diff --git a/Windows/Debugger/CtrlDisAsmView.cpp b/Windows/Debugger/CtrlDisAsmView.cpp index 12ad903ed0..548a204c94 100644 --- a/Windows/Debugger/CtrlDisAsmView.cpp +++ b/Windows/Debugger/CtrlDisAsmView.cpp @@ -802,7 +802,7 @@ void CtrlDisAsmView::onKeyDown(WPARAM wParam, LPARAM lParam) u32 windowEnd = windowStart+visibleRows*instructionSize; keyTaken = true; - if (GetAsyncKeyState(VK_CONTROL)) + if (KeyDownAsync(VK_CONTROL)) { switch (tolower(wParam & 0xFFFF)) { @@ -836,11 +836,11 @@ void CtrlDisAsmView::onKeyDown(WPARAM wParam, LPARAM lParam) switch (wParam & 0xFFFF) { case VK_DOWN: - setCurAddress(curAddress + instructionSize, GetAsyncKeyState(VK_SHIFT) != 0); + setCurAddress(curAddress + instructionSize, KeyDownAsync(VK_SHIFT) != 0); scrollAddressIntoView(); break; case VK_UP: - setCurAddress(curAddress - instructionSize, GetAsyncKeyState(VK_SHIFT) != 0); + setCurAddress(curAddress - instructionSize, KeyDownAsync(VK_SHIFT) != 0); scrollAddressIntoView(); break; case VK_NEXT: @@ -848,16 +848,16 @@ void CtrlDisAsmView::onKeyDown(WPARAM wParam, LPARAM lParam) setCurAddress(windowEnd - instructionSize, GetAsyncKeyState(VK_SHIFT) != 0); scrollAddressIntoView(); } else { - setCurAddress(curAddress + visibleRows * instructionSize, GetAsyncKeyState(VK_SHIFT) != 0); + setCurAddress(curAddress + visibleRows * instructionSize, KeyDownAsync(VK_SHIFT) != 0); scrollAddressIntoView(); } break; case VK_PRIOR: if (curAddress != windowStart && curAddressIsVisible()) { - setCurAddress(windowStart, GetAsyncKeyState(VK_SHIFT) != 0); + setCurAddress(windowStart, KeyDownAsync(VK_SHIFT) != 0); scrollAddressIntoView(); } else { - setCurAddress(curAddress - visibleRows * instructionSize, GetAsyncKeyState(VK_SHIFT) != 0); + setCurAddress(curAddress - visibleRows * instructionSize, KeyDownAsync(VK_SHIFT) != 0); scrollAddressIntoView(); } break; @@ -957,7 +957,7 @@ void CtrlDisAsmView::onMouseDown(WPARAM wParam, LPARAM lParam, int button) int y = HIWORD(lParam); u32 newAddress = yToAddress(y); - bool extend = GetAsyncKeyState(VK_SHIFT) != 0; + bool extend = KeyDownAsync(VK_SHIFT) != 0; if (button == 1) { if (newAddress == curAddress && hasFocus) @@ -983,7 +983,7 @@ void CtrlDisAsmView::onMouseUp(WPARAM wParam, LPARAM lParam, int button) { int x = LOWORD(lParam); int y = HIWORD(lParam); - setCurAddress(yToAddress(y), GetAsyncKeyState(VK_SHIFT) != 0); + setCurAddress(yToAddress(y), KeyDownAsync(VK_SHIFT) != 0); redraw(); } else if (button == 2) @@ -1152,7 +1152,7 @@ void CtrlDisAsmView::onMouseMove(WPARAM wParam, LPARAM lParam, int button) { int x = LOWORD(lParam); int y = HIWORD(lParam); - setCurAddress(yToAddress(y), GetAsyncKeyState(VK_SHIFT) != 0); + setCurAddress(yToAddress(y), KeyDownAsync(VK_SHIFT) != 0); // TODO: Perhaps don't do this every time, but on a timer? redraw(); } @@ -1287,7 +1287,7 @@ void CtrlDisAsmView::search(bool continueSearch) } // cancel search - if ((searchAddress % 256) == 0 && GetAsyncKeyState(VK_ESCAPE)) + if ((searchAddress % 256) == 0 && KeyDownAsync(VK_ESCAPE)) { searching = false; return; diff --git a/Windows/Debugger/CtrlMemView.cpp b/Windows/Debugger/CtrlMemView.cpp index d737cf637d..1554a9cf0b 100644 --- a/Windows/Debugger/CtrlMemView.cpp +++ b/Windows/Debugger/CtrlMemView.cpp @@ -302,7 +302,7 @@ void CtrlMemView::onVScroll(WPARAM wParam, LPARAM lParam) void CtrlMemView::onKeyDown(WPARAM wParam, LPARAM lParam) { - if (GetAsyncKeyState(VK_CONTROL)) + if (KeyDownAsync(VK_CONTROL)) { switch (tolower(wParam & 0xFFFF)) { @@ -354,7 +354,7 @@ void CtrlMemView::onKeyDown(WPARAM wParam, LPARAM lParam) void CtrlMemView::onChar(WPARAM wParam, LPARAM lParam) { - if (GetAsyncKeyState(VK_CONTROL) || wParam == VK_TAB) return; + if (KeyDownAsync(VK_CONTROL) || wParam == VK_TAB) return; if (!Memory::IsValidAddress(curAddress)) { @@ -649,12 +649,12 @@ void CtrlMemView::search(bool continueSearch) while (index < endIndex) { // cancel search - if ((index % 256) == 0 && GetAsyncKeyState(VK_ESCAPE)) + if ((index % 256) == 0 && KeyDownAsync(VK_ESCAPE)) { searching = false; return; } - + if (memcmp(&dataPointer[index],searchData.data(),searchData.size()) == 0) { matchAddress = index+segmentStart; diff --git a/Windows/GEDebugger/CtrlDisplayListView.cpp b/Windows/GEDebugger/CtrlDisplayListView.cpp index 02ed013c8c..11ae6be511 100644 --- a/Windows/GEDebugger/CtrlDisplayListView.cpp +++ b/Windows/GEDebugger/CtrlDisplayListView.cpp @@ -264,7 +264,7 @@ void CtrlDisplayListView::onMouseDown(WPARAM wParam, LPARAM lParam, int button) int line = y/rowHeight; u32 newAddress = windowStart + line*instructionSize; - bool extend = GetAsyncKeyState(VK_SHIFT) != 0; + bool extend = KeyDownAsync(VK_SHIFT) != 0; if (button == 1) { if (newAddress == curAddress && hasFocus) @@ -412,28 +412,28 @@ void CtrlDisplayListView::onKeyDown(WPARAM wParam, LPARAM lParam) switch (wParam & 0xFFFF) { case VK_DOWN: - setCurAddress(curAddress + instructionSize, GetAsyncKeyState(VK_SHIFT) != 0); + setCurAddress(curAddress + instructionSize, KeyDownAsync(VK_SHIFT) != 0); scrollAddressIntoView(); break; case VK_UP: - setCurAddress(curAddress - instructionSize, GetAsyncKeyState(VK_SHIFT) != 0); + setCurAddress(curAddress - instructionSize, KeyDownAsync(VK_SHIFT) != 0); scrollAddressIntoView(); break; case VK_NEXT: if (curAddress != windowEnd - instructionSize && curAddressIsVisible()) { - setCurAddress(windowEnd - instructionSize, GetAsyncKeyState(VK_SHIFT) != 0); + setCurAddress(windowEnd - instructionSize, KeyDownAsync(VK_SHIFT) != 0); scrollAddressIntoView(); } else { - setCurAddress(curAddress + visibleRows * instructionSize, GetAsyncKeyState(VK_SHIFT) != 0); + setCurAddress(curAddress + visibleRows * instructionSize, KeyDownAsync(VK_SHIFT) != 0); scrollAddressIntoView(); } break; case VK_PRIOR: if (curAddress != windowStart && curAddressIsVisible()) { - setCurAddress(windowStart, GetAsyncKeyState(VK_SHIFT) != 0); + setCurAddress(windowStart, KeyDownAsync(VK_SHIFT) != 0); scrollAddressIntoView(); } else { - setCurAddress(curAddress - visibleRows * instructionSize, GetAsyncKeyState(VK_SHIFT) != 0); + setCurAddress(curAddress - visibleRows * instructionSize, KeyDownAsync(VK_SHIFT) != 0); scrollAddressIntoView(); } break; diff --git a/Windows/W32Util/Misc.h b/Windows/W32Util/Misc.h index 03f121066f..8189a7e20d 100644 --- a/Windows/W32Util/Misc.h +++ b/Windows/W32Util/Misc.h @@ -17,6 +17,14 @@ struct GenericListViewColumn float size; }; +// the most significant bit states whether the key is currently down. +// simply checking if it's != 0 is not enough, as bit0 is set if +// the key was pressed between the last call to GetAsyncKeyState +inline bool KeyDownAsync(int vkey) +{ + return (GetAsyncKeyState(vkey) & 0x8000) != 0; +} + class GenericListControl { public: From 0e619138394fb52c0b320246648691106181657a Mon Sep 17 00:00:00 2001 From: Kingcom Date: Tue, 5 Nov 2013 11:34:53 +0100 Subject: [PATCH 2/2] Remove redundant != 0 checks --- Windows/Debugger/CtrlDisAsmView.cpp | 18 +++++++++--------- Windows/GEDebugger/CtrlDisplayListView.cpp | 14 +++++++------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/Windows/Debugger/CtrlDisAsmView.cpp b/Windows/Debugger/CtrlDisAsmView.cpp index 548a204c94..e787aaa383 100644 --- a/Windows/Debugger/CtrlDisAsmView.cpp +++ b/Windows/Debugger/CtrlDisAsmView.cpp @@ -836,28 +836,28 @@ void CtrlDisAsmView::onKeyDown(WPARAM wParam, LPARAM lParam) switch (wParam & 0xFFFF) { case VK_DOWN: - setCurAddress(curAddress + instructionSize, KeyDownAsync(VK_SHIFT) != 0); + setCurAddress(curAddress + instructionSize, KeyDownAsync(VK_SHIFT)); scrollAddressIntoView(); break; case VK_UP: - setCurAddress(curAddress - instructionSize, KeyDownAsync(VK_SHIFT) != 0); + setCurAddress(curAddress - instructionSize, KeyDownAsync(VK_SHIFT)); scrollAddressIntoView(); break; case VK_NEXT: if (curAddress != windowEnd - instructionSize && curAddressIsVisible()) { - setCurAddress(windowEnd - instructionSize, GetAsyncKeyState(VK_SHIFT) != 0); + setCurAddress(windowEnd - instructionSize, GetAsyncKeyState(VK_SHIFT)); scrollAddressIntoView(); } else { - setCurAddress(curAddress + visibleRows * instructionSize, KeyDownAsync(VK_SHIFT) != 0); + setCurAddress(curAddress + visibleRows * instructionSize, KeyDownAsync(VK_SHIFT)); scrollAddressIntoView(); } break; case VK_PRIOR: if (curAddress != windowStart && curAddressIsVisible()) { - setCurAddress(windowStart, KeyDownAsync(VK_SHIFT) != 0); + setCurAddress(windowStart, KeyDownAsync(VK_SHIFT)); scrollAddressIntoView(); } else { - setCurAddress(curAddress - visibleRows * instructionSize, KeyDownAsync(VK_SHIFT) != 0); + setCurAddress(curAddress - visibleRows * instructionSize, KeyDownAsync(VK_SHIFT)); scrollAddressIntoView(); } break; @@ -957,7 +957,7 @@ void CtrlDisAsmView::onMouseDown(WPARAM wParam, LPARAM lParam, int button) int y = HIWORD(lParam); u32 newAddress = yToAddress(y); - bool extend = KeyDownAsync(VK_SHIFT) != 0; + bool extend = KeyDownAsync(VK_SHIFT); if (button == 1) { if (newAddress == curAddress && hasFocus) @@ -983,7 +983,7 @@ void CtrlDisAsmView::onMouseUp(WPARAM wParam, LPARAM lParam, int button) { int x = LOWORD(lParam); int y = HIWORD(lParam); - setCurAddress(yToAddress(y), KeyDownAsync(VK_SHIFT) != 0); + setCurAddress(yToAddress(y), KeyDownAsync(VK_SHIFT)); redraw(); } else if (button == 2) @@ -1152,7 +1152,7 @@ void CtrlDisAsmView::onMouseMove(WPARAM wParam, LPARAM lParam, int button) { int x = LOWORD(lParam); int y = HIWORD(lParam); - setCurAddress(yToAddress(y), KeyDownAsync(VK_SHIFT) != 0); + setCurAddress(yToAddress(y), KeyDownAsync(VK_SHIFT)); // TODO: Perhaps don't do this every time, but on a timer? redraw(); } diff --git a/Windows/GEDebugger/CtrlDisplayListView.cpp b/Windows/GEDebugger/CtrlDisplayListView.cpp index 11ae6be511..cd3c9ae7d9 100644 --- a/Windows/GEDebugger/CtrlDisplayListView.cpp +++ b/Windows/GEDebugger/CtrlDisplayListView.cpp @@ -264,7 +264,7 @@ void CtrlDisplayListView::onMouseDown(WPARAM wParam, LPARAM lParam, int button) int line = y/rowHeight; u32 newAddress = windowStart + line*instructionSize; - bool extend = KeyDownAsync(VK_SHIFT) != 0; + bool extend = KeyDownAsync(VK_SHIFT); if (button == 1) { if (newAddress == curAddress && hasFocus) @@ -412,28 +412,28 @@ void CtrlDisplayListView::onKeyDown(WPARAM wParam, LPARAM lParam) switch (wParam & 0xFFFF) { case VK_DOWN: - setCurAddress(curAddress + instructionSize, KeyDownAsync(VK_SHIFT) != 0); + setCurAddress(curAddress + instructionSize, KeyDownAsync(VK_SHIFT)); scrollAddressIntoView(); break; case VK_UP: - setCurAddress(curAddress - instructionSize, KeyDownAsync(VK_SHIFT) != 0); + setCurAddress(curAddress - instructionSize, KeyDownAsync(VK_SHIFT)); scrollAddressIntoView(); break; case VK_NEXT: if (curAddress != windowEnd - instructionSize && curAddressIsVisible()) { - setCurAddress(windowEnd - instructionSize, KeyDownAsync(VK_SHIFT) != 0); + setCurAddress(windowEnd - instructionSize, KeyDownAsync(VK_SHIFT)); scrollAddressIntoView(); } else { - setCurAddress(curAddress + visibleRows * instructionSize, KeyDownAsync(VK_SHIFT) != 0); + setCurAddress(curAddress + visibleRows * instructionSize, KeyDownAsync(VK_SHIFT)); scrollAddressIntoView(); } break; case VK_PRIOR: if (curAddress != windowStart && curAddressIsVisible()) { - setCurAddress(windowStart, KeyDownAsync(VK_SHIFT) != 0); + setCurAddress(windowStart, KeyDownAsync(VK_SHIFT)); scrollAddressIntoView(); } else { - setCurAddress(curAddress - visibleRows * instructionSize, KeyDownAsync(VK_SHIFT) != 0); + setCurAddress(curAddress - visibleRows * instructionSize, KeyDownAsync(VK_SHIFT)); scrollAddressIntoView(); } break;