# scorpiox-win32-keyboard-hook-x11-keysym-input

Windows-only, pure C (Win32 API, zero third-party deps) DLL that captures
keyboard input for a given top-level window, translates it to **X11 keysyms**
using the user's **active keyboard layout** (via `ToUnicodeEx`), and delivers
`(keysym, down)` to a caller-supplied C callback.

It knows **nothing about VNC** — VNC is just one consumer. The callback type
`int (*)(void*, uint32_t keysym, int down)` is deliberately identical in shape
to `scorpiox-vnc.dll`'s `vnc_send_key` so an app can pass that export's
address directly.

**Platform: win-x64 ONLY. There is no Linux build** — the DLL wraps
`WH_KEYBOARD_LL` (see `exports.json` `"platforms": ["win-x64"]`).

## Why a separate DLL (2026-09-29)

The FastVNCViewer WinUI3 keyboard path was "too buggy": WinUI 3 only routes
keys to a focused Control, focus falls out on popups/window switches, Win/Alt+Tab
never reach the page, and a hand-written US-only VK table was wrong for
modifiers. This DLL does the capture in pure C with a low-level hook that sees
**every** key (including injected input and, when the target window is
foreground, system chords), and lets Windows do the layout translation.

## Architecture

```
App (WinUI3 today, anything later)
  ctx = vnc_create(); vnc_connect(...)
  kbd_attach(hwnd, callback = &vnc_send_key, user = ctx)   ← one-time wiring
                         │
THIS DLL: own thread + message loop
  WH_KEYBOARD_LL hook → gate: GetForegroundWindow()==hwnd && active
  → translate (layout-aware) → bounded queue
  → sender thread → callback(user, keysym, down)  ───►  scorpiox-vnc vnc_send_key
```

1. **No link to scorpiox-vnc.** No DLL-to-DLL dependency.
2. **The hook thread never blocks.** Windows silently removes a low-level hook
   whose callback exceeds `LowLevelHooksTimeout` (~300 ms+). The hook proc only
   translates + enqueues; a dedicated **sender thread** drains the queue and
   calls the callback (network I/O lives there).
3. **Own the thread.** `kbd_attach` spins a thread that installs the hook and
   runs `GetMessage`; the caller's UI thread is never involved. `kbd_detach`
   posts `WM_QUIT`, joins, unhooks.
4. **Scope gate:** keys are only swallowed/forwarded when `active` AND the
   foreground window is `hwnd` (or owned by it). Otherwise everything is
   passed through untouched — never eat keys for other apps.
5. **Output = X11 keysyms** (X11/keysymdef.h), the neutral standard.

## Layout correctness (important)

The LL hook's `vkCode` is produced by the system using the layout of the
thread that receives the hook. `ToUnicodeEx` then maps VK→char under the HKL
you pass. So **the hook thread runs under the target window thread's layout**,
mirrored on every event: if the user switches layouts on the target thread,
this DLL re-activates it on the hook thread. Verified on .204 (2026-10-01):
German target window + US-layout hook thread → hook sees `vk=0x59` for the
physical Y position and `ToUnicodeEx` under the German layout returns `z`.

## Exports

| Export | Signature |
|---|---|
| `kbd_version` | `const char* kbd_version(void)` |
| `kbd_last_error` | `const char* kbd_last_error(void)` |
| `kbd_attach` | `void* kbd_attach(void *hwnd, kbd_key_cb cb, void *user)` |
| `kbd_detach` | `void kbd_detach(void *h)` — releases held keys first |
| `kbd_set_active` | `int kbd_set_active(void *h, int active)` — 0 also releases held keys |
| `kbd_set_capture_system_keys` | `int kbd_set_capture_system_keys(void *h, int on)` — Win, Alt+Tab, Alt+Esc, Ctrl+Esc → remote; default ON |
| `kbd_release_all` | `int kbd_release_all(void *h)` — key-up for every held key, modifiers last |
| `kbd_send_ctrl_alt_del` | `int kbd_send_ctrl_alt_del(void *h)` — synthesized via callback (OS never delivers SAS to LL hooks) |
| `kbd_get_stats` | `int kbd_get_stats(void *h, char *json_out, int cap)` |
| `kbd_test_inject` | `int kbd_test_inject(void *h, uint32_t vk, uint32_t scancode, uint32_t flags, int down)` — test-only, same translate+queue path |
| `kbd_test_translate` | `uint32_t kbd_test_translate(uint32_t vk, uint32_t scancode, int extended, int shift, int altgr, const char *klid)` — test-only, stateless |

`typedef int (*kbd_key_cb)(void *user, uint32_t keysym, int down);`

## Translation rules (implemented exactly per spec)

1. **Non-character keys** → fixed VK + scancode + extended table
   (BackSpace FF08 … arrows FF51–FF54, F1–F24, KP_* FFB0+, modifiers FFE1+).
   L/R distinguished by scancode / `LLKHF_EXTENDED`.
2. **Character keys** → `ToUnicodeEx` with the target window thread's HKL and
   a key-state array built from the hook's own tracked modifier state
   (never `GetKeyboardState` from a foreign thread).
   Latin-1 → code point; else `0x01000000 | cp`.
3. **Dead keys:** `ToUnicodeEx` returns -1 → emit the X11 dead keysym
   (`dead_acute` FE51, `dead_grave` FE50, `dead_circumflex` FE52,
   `dead_tilde` FE53, `dead_diaeresis` FE57 — mapped by priming then reading
   the combining mark off `e`); server composes, we do not.
4. **Ctrl chords:** when Ctrl (not AltGr) is held and `ToUnicodeEx` yields a
   control char, send the **unshifted base keysym** instead
   (Ctrl+C → Control_L + `c`).
5. **Key-up** sends the SAME keysym remembered from its key-down
   (per scancode — layout/modifiers may have changed in between).
6. **Auto-repeat** forwards as-is.
7. **Right Alt** = `ISO_Level3_Shift` FE03 when the layout has AltGr
   (probed: alt+ctrl differs from plain on Q), else `Alt_R` FFEA.
8. Unmapped → dropped, counted in `dropped_unmapped`.

## Build

Windows only (`.204` — `C:\gcc16\mingw64`):

```bat
mingw32-make dll
rem deps must be exactly KERNEL32 + USER32 + CRT:
objdump -p scorpiox-win32-keyboard-hook-x11-keysym-input.dll | findstr "DLL Name"
```

## Test

C# console test repo: `scorpiox-win32-keyboard-hook-x11-keysym-input.console`
(`--test` runs K1–K14; K13/K14 need an interactive session on .204).

## Dist

`dist.scorpiox.net/dlls/scorpiox-win32-keyboard-hook-x11-keysym-input/{build,verified}/1.0.0/`
(`latest` → verified/1.0.0). Only the `.dll` + `exports.json`.
