RefocusGuard
Consumes refocus mouse input and blocks local attack/placement gameplay endpoints until the activation click is safely released.
About this mod Author’s original text
Refocus Guard
A tiny client-side Valheim mod for one annoyance:
When Valheim regains input focus after Alt-Tab, the mouse click used to activate
the game is consumed instead of becoming an in-game action.
What is protected
While the activation click is being consumed:
- Left mouse cannot trigger Attack.
- Right mouse cannot trigger Block or BuildMenu.
- Middle mouse cannot trigger SecondaryAttack or Remove.
- Matching raw ZInput mouse-button reads are also suppressed.
- Player.PlayerAttackInput is skipped during the refocus guard.
- Player.UpdatePlacement is skipped during the refocus guard.
- Humanoid.StartAttack has a local-player safety net during the guard.
Once the pressed activation button is released, controls return to normal
immediately.
Why there is a short safety window
Unity can expose focus gain and the activation mouse edge in an awkward order
inside the same frame. Refocus Guard therefore suppresses the three mouse
buttons for 0.15 seconds after refocus, latches any activation button that is
actually held, and keeps only that held button suppressed until release.
A highest-priority Player.Update prefix is also used so the guard is armed
before Valheim reads Attack/Block/SecondaryAttack in the first focused frame.
Normal focus/input events are intentionally not logged; only the startup message
is emitted during ordinary operation.
What it does not do
Refocus Guard does not force the Windows foreground window to accept keyboard
or mouse input. Windows may occasionally bring Valheim to the foreground
without giving it full input focus until you click the window.
That activation click is exactly what this mod makes safe.
The mod does not:
- modify attacks or combat after refocus is complete,
- change keybinds,
- add config,
- add prefabs/items/RPCs/save data,
- require installation on the server.
Test
- Start Valheim and enter a world.
- Equip a weapon or tool.
- Alt-Tab to another application.
- Click Valheim with left mouse.
- The game should regain focus without attacking or swinging the tool.
- Release left mouse, then click normally; the attack should work immediately.
- Repeat with right mouse; it should not block/open the build menu from the
activation click.
- Repeat with middle mouse while holding a hammer; it should not remove a
piece from the activation click.
- Alt-Tab back using only the keyboard. Normal mouse input should work after
the tiny two-frame focus grace period.
Install & use Install: BepInEx plugin
- The archive from playbuff is the mod itself — usually a BepInEx plugin (a .dll or a plugin folder once unzipped).
- Recommended: use a community mod manager (such as r2modman) — pick this game, import the mod, and the manager sets up BepInEx and its dependencies for you.
- Manual route: install the BepInEx loader in the game folder, then drop the unzipped .dll or plugin folder into BepInEx/plugins/.
- Launch the game — the BepInEx console logs every plugin it loads; seeing this mod in the log means it is installed.
Mods with listed dependencies need those installed too, or they will not load. Options are usually changed in-game or in the plugin's config file. In multiplayer, the host and every client need the same mods installed.
Install these first, or the mod will not work: