Skip to content

Commit

Permalink
platform: adapt win-platform for global detect (#16)
Browse files Browse the repository at this point in the history
- #14

adapt win-platform for global detect and test

Signed-off-by: Sunrisepeak <speakshen@163.com>
  • Loading branch information
Sunrisepeak authored Mar 25, 2024
1 parent 5a3d3f5 commit 38b4412
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
8 changes: 5 additions & 3 deletions platform/win_platform_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ LRESULT CALLBACK KeyboardHookProc(int nCode, WPARAM wParam, LPARAM lParam); // k

void PAL::platformInit() {
PAL::gamepadConnected = false;
// globalDetectWinID is a flag, isn't real window id
PAL::globalDetectWinID = 'K' + 'H' + 'i' + 's' + 't' + 'o' + 'r' + 'y';
gTargetWindowID = 0;
gKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardHookProc, NULL, 0);
Expand Down Expand Up @@ -95,15 +96,15 @@ dstruct::Vector<PAL::KeyData> PAL::platformKeyDetect(unsigned int wID) {
gTargetWindowID = wID;
}

HWND hWnd = GetForegroundWindow();

{ // gamepad
bool connected = false;
DWORD dwResult;
XINPUT_STATE state;
ZeroMemory(&state, sizeof(XINPUT_STATE));
DWORD numControllers = XUSER_MAX_COUNT;

HWND hWnd = GetForegroundWindow();

// gamepad connect check
for (DWORD i = 0; i < numControllers && reinterpret_cast<unsigned int>(hWnd) == gTargetWindowID; ++i) {
dwResult = XInputGetState(i, &state);
Expand Down Expand Up @@ -139,7 +140,8 @@ dstruct::Vector<PAL::KeyData> PAL::platformKeyDetect(unsigned int wID) {
if (!PAL::gamepadConnected) { // keyboard
MSG msg; // translate msg to hook func
int msgLimit = 10;
while (PeekMessage(&msg, reinterpret_cast<HWND>(gTargetWindowID), 0, 0, PM_REMOVE) > 0 && msgLimit--) {
HWND wId = PAL::globalDetectWinID == gTargetWindowID ? hWnd : reinterpret_cast<HWND>(gTargetWindowID);
while (PeekMessage(&msg, wId, 0, 0, PM_REMOVE) > 0 && msgLimit--) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
Expand Down
6 changes: 6 additions & 0 deletions src/KeyHistory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,16 @@ void KeyHistory::_drawControlImpl() {

if (ImGui::BeginCombo(" <- window", windowName.c_str(), 0)) {
dstruct::Vector<PAL::WindowInfo> currentWindowList = TargetWindowKeyDetect::getInstance().getWindowInfoList();
PAL::WindowInfo wInfo;
wInfo.id = PAL::globalDetectWinID;
wInfo.name = "Global Detect(Note: don't use when input passwd)";
currentWindowList.push_back(wInfo);
/* error C7555: need std:c++20
currentWindowList.push_back(PAL::WindowInfo{
.id = PAL::globalDetectWinID,
.name = "Global Detect(Note: don't use when input passwd)"
});
*/
TargetWindowKeyDetect::getInstance().setTargetWindow({0, ""});
isListening = false;
for (int n = 0; n < currentWindowList.size(); n++) {
Expand Down

0 comments on commit 38b4412

Please sign in to comment.