Valheim
Install with App

Details

Latest version
0.1.3
Last Updated
First Uploaded
Downloads
52
Likes
0
Size
22KB
Dependants
ADDatHost Valheim hosting
30% off

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

  1. Start Valheim and enter a world.
  2. Equip a weapon or tool.
  3. Alt-Tab to another application.
  4. Click Valheim with left mouse.
  5. The game should regain focus without attacking or swinging the tool.
  6. Release left mouse, then click normally; the attack should work immediately.
  7. Repeat with right mouse; it should not block/open the build menu from the activation click.
  8. Repeat with middle mouse while holding a hammer; it should not remove a piece from the activation click.
  9. Alt-Tab back using only the keyboard. Normal mouse input should work after the tiny two-frame focus grace period.
Thunderstore development is made possible with ads. Please consider making an exception to your adblock.