Skip to content

Commit b18b936

Browse files
committed
fix: Gamepad 强制前台
1 parent 81faa12 commit b18b936

2 files changed

Lines changed: 13 additions & 9 deletions

File tree

source/MaaGamepadControlUnit/Manager/GamepadControlUnitMgr.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,17 @@ bool GamepadControlUnitMgr::init_win32_unit()
141141
return true;
142142
}
143143

144-
void GamepadControlUnitMgr::send_activate()
144+
void GamepadControlUnitMgr::ensure_foreground()
145145
{
146146
if (!hwnd_) {
147147
return;
148148
}
149-
// 发送 WM_ACTIVATE + WA_ACTIVE,让目标窗口认为自己被激活
150-
SendMessageW(hwnd_, WM_ACTIVATE, WA_ACTIVE, 0);
149+
150+
if (hwnd_ == GetForegroundWindow()) {
151+
return;
152+
}
153+
154+
SetForegroundWindow(hwnd_);
151155
}
152156

153157
bool GamepadControlUnitMgr::connected() const
@@ -223,7 +227,7 @@ bool GamepadControlUnitMgr::touch_down(int contact, int x, int y, int pressure)
223227
return false;
224228
}
225229

226-
send_activate();
230+
ensure_foreground();
227231

228232
switch (contact) {
229233
case MaaGamepadTouch_LeftStick:
@@ -291,7 +295,7 @@ bool GamepadControlUnitMgr::key_down(int key)
291295
return false;
292296
}
293297

294-
send_activate();
298+
ensure_foreground();
295299

296300
return gamepad_input_->press_button(key);
297301
}

source/MaaGamepadControlUnit/Manager/GamepadControlUnitMgr.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class GamepadControlUnitMgr : public Win32ControlUnitAPI
1919
GamepadControlUnitMgr(HWND hWnd, MaaGamepadType gamepad_type, MaaWin32ScreencapMethod screencap_method);
2020
virtual ~GamepadControlUnitMgr() override;
2121

22-
public: // from ControlUnitAPI
22+
public: // from ControlUnitAPI
2323
virtual bool connect() override;
2424
virtual bool connected() const override;
2525

@@ -54,7 +54,7 @@ class GamepadControlUnitMgr : public Win32ControlUnitAPI
5454

5555
private:
5656
bool init_win32_unit();
57-
void send_activate();
57+
void ensure_foreground();
5858

5959
private:
6060
HWND hwnd_ = nullptr;
@@ -64,8 +64,8 @@ class GamepadControlUnitMgr : public Win32ControlUnitAPI
6464
bool connected_ = false;
6565

6666
std::unique_ptr<ViGEmInput> gamepad_input_;
67-
std::unique_ptr<Win32ControlUnitLoader> win32_loader_; // DLL loader
68-
std::shared_ptr<ControlUnitAPI> win32_unit_; // for screencap
67+
std::unique_ptr<Win32ControlUnitLoader> win32_loader_; // DLL loader
68+
std::shared_ptr<ControlUnitAPI> win32_unit_; // for screencap
6969
};
7070

7171
MAA_CTRL_UNIT_NS_END

0 commit comments

Comments
 (0)