# NUI focus, the mouse cursor and keyboard input

> Handle NUI focus in FiveM correctly: SetNuiFocus parameters, SetNuiFocusKeepInput for walking with a UI open, disabling camera and attack controls, closing with Escape, the pause-menu conflict, and fixing focus that stays stuck after a resource restart.

Source: https://fam-tools.com/guides/fivem-nui-focus-and-cursor · FAM Tools · published 2026-09-11 · updated 2026-09-11 · about 9 min

**Short answer.** `SetNuiFocus(hasFocus, hasCursor)` sends keyboard (first argument) and mouse cursor (second) to your page. `SetNuiFocusKeepInput(true)` lets the game keep receiving input too, so players can walk with a UI open — then disable camera, attack and pause controls yourself. Always release focus when closing, including on Escape and when the resource stops.

## Key points

- `SetNuiFocus(true, true)` = keyboard + cursor to the UI.
- `SetNuiFocus(false, false)` on every close path.
- `SetNuiFocusKeepInput(true)` = walk while the UI is open.
- With KeepInput, disable look (1, 2), attack (24, 257), aim (25) and pause (199, 200).
- Release focus in `onResourceStop` to avoid a stuck cursor.

A menu that opens but cannot be clicked, a cursor that never goes away, a player who can shoot while typing in a phone — all of these are focus bugs. NUI focus decides whether keyboard and mouse go to your page or to the game, and three natives cover almost everything.

## SetNuiFocus

| Call | Result |
| --- | --- |
| `SetNuiFocus(true, true)` | Keyboard and mouse to the UI — menus, forms |
| `SetNuiFocus(true, false)` | Keyboard to the UI, no cursor |
| `SetNuiFocus(false, false)` | Everything back to the game |

`IsNuiFocused()` tells you whether any NUI currently has focus — useful before opening a second UI.

## Closing properly

*app.js — Escape closes and tells Lua*

```js
window.addEventListener('keydown', (e) => {
  if (e.key === 'Escape') {
    document.body.classList.remove('open');
    fetch(`https://${GetParentResourceName()}/close`, { method: 'POST', body: '{}' });
  }
});
```

*client.lua*

```lua
RegisterNuiCallback('close', function(_, cb)
    SetNuiFocus(false, false)
    cb('ok')
end)

AddEventHandler('onResourceStop', function(res)
    if res == GetCurrentResourceName() then
        SetNuiFocus(false, false)
    end
end)
```

## Walking with a UI open

Phones and radial HUDs often let players keep moving. `SetNuiFocusKeepInput(true)` keeps game input active while the UI has focus — including the camera and the fire button, so disable what should not work.

*client.lua*

```lua
local open = false

local function setOpen(state)
    open = state
    SetNuiFocus(state, state)
    SetNuiFocusKeepInput(state)
    SendNUIMessage({ action = state and 'open' or 'close' })
    if not state then return end

    CreateThread(function()
        while open do
            DisableControlAction(0, 1, true)   -- look left/right
            DisableControlAction(0, 2, true)   -- look up/down
            DisableControlAction(0, 24, true)  -- attack
            DisableControlAction(0, 25, true)  -- aim
            DisableControlAction(0, 257, true) -- attack 2
            DisableControlAction(0, 199, true) -- pause (P)
            DisableControlAction(0, 200, true) -- pause (Esc)
            Wait(0)
        end
    end)
end
```

> **Typing in text fields** — With KeepInput on, keys typed into an input also reach the game — W moves the player while they type. Turn KeepInput off while a text field is focused (send a callback on `focus`/`blur`).

## Fixing stuck focus

1. Make sure every close path (button, Escape, event, death) calls `SetNuiFocus(false, false)`.
2. Release focus in `onResourceStop` — restarting a resource with focus on leaves the cursor behind.
3. Check for two resources fighting: if another UI takes focus and releases it, yours may lose it.
4. Use `IsNuiFocused()` in a debug command to see the current state.

## Loading screens are different

A loading screen has no `SetNuiFocus` — the cursor is enabled with `loadscreen_cursor 'yes'` in the fxmanifest. See [interactive loading screens](https://fam-tools.com/guides/fivem-loading-screen-cursor-interactive).

## Questions

### What do the two SetNuiFocus arguments mean?

The first gives keyboard focus to the UI, the second shows the mouse cursor.

### Why is my cursor stuck on screen?

Focus was never released. Call `SetNuiFocus(false, false)` on every close path and in `onResourceStop`.

### How can players walk with a UI open?

Use `SetNuiFocusKeepInput(true)` and disable the camera, attack and pause controls while it is open.

### Why does Escape open the pause menu with my UI?

With KeepInput on, the game still receives Escape. Disable controls 199 and 200 while the UI is open.

## Related

- https://fam-tools.com/guides/fivem-setnuifocuskeepinput
- https://fam-tools.com/guides/fivem-nui-text-input-keyboard
- https://fam-tools.com/guides/fivem-nui-basics
- https://fam-tools.com/guides/fivem-nui-callbacks-fetch
- https://fam-tools.com/guides/fivem-loading-screen-cursor-interactive
- https://fam-tools.com/guides/fivem-nui-devtools-debugging
