- Add per-VM `capture_host_shortcuts` option persisted in `.reims-go-keymap.json` (independent from modifier preset and backward-compatible with legacy configs and Wails callers omitting the field). When enabled, unmapped Win and Alt keys route through the existing Guest Tools v8 hidutil carrier (`lcmd`/`rcmd`/`lopt`/ `ropt`) while the VM is focused, preventing host Win/Alt shortcuts (e.g. PixPin Alt+A, Alt+Tab, Alt+F4, Ctrl+Alt+A, Win+...) without modifying explicit custom bindings or bumping Guest Tools version. - Monitor `EVENT_SYSTEM_FOREGROUND` and `EVENT_OBJECT_NAMECHANGE` on the Windows keymap hook thread to immediately release held modifiers on focus loss and promote `WH_KEYBOARD_LL` back to the top of the hook chain on VM focus entry and SDL3 in-window re-capture, with per-key timestamp filtering for delayed focus-loss events, swap-window event deduplication, Win64 32-bit parameter narrowing, and fallback timer recovery. - Serialize `SetVMKeymap` read-merge-save under `keymapMu`, expose the toggle in Settings with zh-CN/en-US i18n, and add Go + frontend unit tests.
263 lines
6.8 KiB
Go
263 lines
6.8 KiB
Go
package host
|
|
|
|
import "maps"
|
|
|
|
const (
|
|
vkLWin = 0x5B
|
|
vkRWin = 0x5C
|
|
vkLCtrl = 0xA2
|
|
vkRCtrl = 0xA3
|
|
vkLAlt = 0xA4
|
|
vkRAlt = 0xA5
|
|
)
|
|
|
|
type KeymapRule struct {
|
|
VMName string
|
|
Bindings map[string]string
|
|
CarrierReady bool
|
|
}
|
|
|
|
type KeyEvent struct {
|
|
VK uint32
|
|
ScanCode uint32
|
|
Up bool
|
|
}
|
|
|
|
type EmittedKey struct {
|
|
VK uint16
|
|
Scan uint16
|
|
Extended bool
|
|
Up bool
|
|
}
|
|
|
|
var hostModifiers = map[uint32]string{
|
|
vkLWin: "lwin", vkRWin: "rwin", vkLCtrl: "lctrl", vkRCtrl: "rctrl",
|
|
vkLAlt: "lalt", vkRAlt: "ralt", 0x14: "capslock",
|
|
}
|
|
|
|
var keymapTargets = map[string]EmittedKey{
|
|
"lcmd": {VK: 0x84, Scan: 0x6C}, "rcmd": {VK: 0x85, Scan: 0x6D},
|
|
"lopt": {VK: 0x83, Scan: 0x6B}, "ropt": {VK: 0x87, Scan: 0x76},
|
|
"lctrl": {VK: vkLCtrl, Scan: 0x1D}, "rctrl": {VK: vkRCtrl, Scan: 0x1D, Extended: true},
|
|
"esc": {VK: 0x1B, Scan: 0x01}, "disabled": {},
|
|
}
|
|
|
|
func (r KeymapRule) target(vk uint32) (EmittedKey, bool) {
|
|
key, ok := keymapTargets[r.Bindings[hostModifiers[vk]]]
|
|
return key, ok && (r.CarrierReady || key.VK < 0x83 || key.VK > 0x87)
|
|
}
|
|
|
|
func (r KeymapRule) active() bool {
|
|
for vk := range hostModifiers {
|
|
if _, ok := r.target(vk); ok {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
type KeymapEngine struct {
|
|
rules map[string]KeymapRule
|
|
physical map[uint32]bool
|
|
stalePhysical map[uint32]bool
|
|
activeMapped map[uint32]EmittedKey
|
|
heldVM string
|
|
altGrCtrlDown bool
|
|
altGrCtrlSuppressed bool
|
|
}
|
|
|
|
func NewKeymapEngine(rules []KeymapRule) *KeymapEngine {
|
|
e := &KeymapEngine{
|
|
physical: make(map[uint32]bool),
|
|
stalePhysical: make(map[uint32]bool),
|
|
activeMapped: make(map[uint32]EmittedKey),
|
|
}
|
|
e.SetRules(rules)
|
|
return e
|
|
}
|
|
|
|
func (e *KeymapEngine) SetRules(rules []KeymapRule) []EmittedKey {
|
|
previous := e.rules[e.heldVM]
|
|
e.rules = make(map[string]KeymapRule, len(rules))
|
|
for _, rule := range rules {
|
|
rule.Bindings = maps.Clone(rule.Bindings)
|
|
e.rules[rule.VMName] = rule
|
|
}
|
|
current := e.rules[e.heldVM]
|
|
if e.heldVM != "" && (previous.CarrierReady != current.CarrierReady || !maps.Equal(previous.Bindings, current.Bindings)) {
|
|
return e.ReleaseAll()
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (e *KeymapEngine) ProcessEvent(ev KeyEvent, vmName string, foregroundMatch bool) (bool, []EmittedKey) {
|
|
vk := ev.VK
|
|
if vk == vkLCtrl && ev.ScanCode&0x200 != 0 {
|
|
// Decide suppression once on the initial press so cross-focus auto-repeat
|
|
// never orphans Windows' synthetic AltGr Ctrl key-up.
|
|
if ev.Up {
|
|
suppress := e.altGrCtrlSuppressed
|
|
e.altGrCtrlDown = false
|
|
e.altGrCtrlSuppressed = false
|
|
return suppress, nil
|
|
}
|
|
if e.altGrCtrlDown {
|
|
return e.altGrCtrlSuppressed, nil
|
|
}
|
|
e.altGrCtrlDown = true
|
|
_, mapped := e.rules[vmName].target(vkRAlt)
|
|
e.altGrCtrlSuppressed = foregroundMatch && mapped
|
|
return e.altGrCtrlSuppressed, nil
|
|
}
|
|
if hostModifiers[vk] == "" {
|
|
return false, nil
|
|
}
|
|
wasDown := e.physical[vk]
|
|
delete(e.stalePhysical, vk)
|
|
if ev.Up {
|
|
delete(e.physical, vk)
|
|
if key, suppressed := e.activeMapped[vk]; suppressed {
|
|
delete(e.activeMapped, vk)
|
|
if key.VK != 0 && !e.targetHeld(key) {
|
|
key.Up = true
|
|
return true, []EmittedKey{key}
|
|
}
|
|
return true, nil
|
|
}
|
|
return false, nil
|
|
}
|
|
e.physical[vk] = true
|
|
if _, suppressed := e.activeMapped[vk]; suppressed {
|
|
return true, nil
|
|
}
|
|
if wasDown || !foregroundMatch {
|
|
return false, nil
|
|
}
|
|
if (vk == vkLWin || vk == vkRWin) &&
|
|
(e.physical[vkLCtrl] || e.physical[vkRCtrl]) &&
|
|
(e.physical[vkLAlt] || e.physical[vkRAlt]) {
|
|
return false, e.restoreCaptureModifiers(vmName)
|
|
}
|
|
key, ok := e.rules[vmName].target(vk)
|
|
if !ok {
|
|
return false, nil
|
|
}
|
|
alreadyHeld := e.targetHeld(key)
|
|
e.activeMapped[vk] = key
|
|
e.heldVM = vmName
|
|
if key.VK == 0 || alreadyHeld {
|
|
return true, nil
|
|
}
|
|
return true, []EmittedKey{key}
|
|
}
|
|
|
|
func (e *KeymapEngine) restoreCaptureModifiers(vmName string) []EmittedKey {
|
|
var emit []EmittedKey
|
|
for _, original := range [...]EmittedKey{
|
|
{VK: vkLCtrl, Scan: 0x1D}, {VK: vkRCtrl, Scan: 0x1D, Extended: true},
|
|
{VK: vkLAlt, Scan: 0x38}, {VK: vkRAlt, Scan: 0x38, Extended: true},
|
|
} {
|
|
vk := uint32(original.VK)
|
|
mapped, suppressed := e.activeMapped[vk]
|
|
if !suppressed || mapped == original {
|
|
continue
|
|
}
|
|
e.activeMapped[vk] = original
|
|
e.heldVM = vmName
|
|
_, targetMapped := e.activeMapped[uint32(mapped.VK)]
|
|
if mapped.VK != 0 && !e.targetHeld(mapped) && (targetMapped || !e.physical[uint32(mapped.VK)]) {
|
|
mapped.Up = true
|
|
emit = append(emit, mapped)
|
|
}
|
|
emit = append(emit, original)
|
|
}
|
|
return emit
|
|
}
|
|
|
|
// ReconcilePhysical clears unsuppressed physical modifiers whose KEYUP was
|
|
// missed (e.g., after Ctrl+Alt+Del or UAC), using a two-tick grace so a key
|
|
// pressed right before Windows updates GetAsyncKeyState is never cleared early.
|
|
// Keys in activeMapped were swallowed by WH_KEYBOARD_LL and never set
|
|
// GetAsyncKeyState, so they are intentionally skipped here and cleared via
|
|
// Reset() on secure-desktop transitions.
|
|
func (e *KeymapEngine) ReconcilePhysical(isDown func(vk uint32) bool) []EmittedKey {
|
|
for vk := range e.physical {
|
|
if _, suppressed := e.activeMapped[vk]; suppressed {
|
|
delete(e.stalePhysical, vk)
|
|
continue
|
|
}
|
|
if isDown(vk) {
|
|
delete(e.stalePhysical, vk)
|
|
continue
|
|
}
|
|
if !e.stalePhysical[vk] {
|
|
e.stalePhysical[vk] = true
|
|
continue
|
|
}
|
|
delete(e.physical, vk)
|
|
delete(e.stalePhysical, vk)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Reset releases every active mapped target key and clears all tracked physical
|
|
// and suppressed key state (used when switching to a Windows Secure Desktop
|
|
// where user-mode WH_KEYBOARD_LL cannot observe key-up events).
|
|
func (e *KeymapEngine) Reset() []EmittedKey {
|
|
emit := e.ReleaseAll()
|
|
clear(e.physical)
|
|
clear(e.stalePhysical)
|
|
clear(e.activeMapped)
|
|
e.heldVM = ""
|
|
e.altGrCtrlDown = false
|
|
e.altGrCtrlSuppressed = false
|
|
return emit
|
|
}
|
|
|
|
func (e *KeymapEngine) HasTrackedKeys() bool {
|
|
return len(e.physical) != 0 || len(e.activeMapped) != 0 || e.altGrCtrlDown || e.altGrCtrlSuppressed
|
|
}
|
|
|
|
func (e *KeymapEngine) targetHeld(key EmittedKey) bool {
|
|
for _, mapped := range e.activeMapped {
|
|
if mapped == key {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func (e *KeymapEngine) ReleaseAll() []EmittedKey {
|
|
return e.ReleaseMatching(func(uint32) bool { return true })
|
|
}
|
|
|
|
func (e *KeymapEngine) ReleaseMatching(shouldRelease func(vk uint32) bool) []EmittedKey {
|
|
var emit []EmittedKey
|
|
hasActive := false
|
|
for vk, key := range e.activeMapped {
|
|
if !shouldRelease(vk) {
|
|
if key.VK != 0 {
|
|
hasActive = true
|
|
}
|
|
continue
|
|
}
|
|
// Keep swallowing the physical key until its matching key-up.
|
|
e.activeMapped[vk] = EmittedKey{}
|
|
if key.VK != 0 && !e.targetHeld(key) {
|
|
key.Up = true
|
|
emit = append(emit, key)
|
|
}
|
|
}
|
|
if !hasActive {
|
|
e.heldVM = ""
|
|
}
|
|
return emit
|
|
}
|
|
|
|
func (e *KeymapEngine) ReleaseIfFocusLost(currentVM string) []EmittedKey {
|
|
if e.heldVM != "" && currentVM != e.heldVM {
|
|
return e.ReleaseAll()
|
|
}
|
|
return nil
|
|
}
|