Files
winauth-go/internal/hotkey/hotkey.go
T
iceking2nd c671f2115e feat: Phase 1 WinAuth Go 移植完整实现
将原 C#/.NET WinAuth 移植为 Go + Gio GUI,覆盖 Phase 1 全部功能。

核心模块:
- internal/authenticator: TOTP (Google/Microsoft/Okta) + HOTP + BattleNet + Steam,含
  enroll/sync/code 生成、Steam 交易确认轮询
- internal/config: YAML 配置 + 老版 WinAuth XML 导入(DPAPI + Password + Blowfish/PBKDF2 解密链)
- internal/crypto: 现代加密 (WAGO1) + DPAPI 跨平台封装 + 老版 Blowfish ECB
- internal/win32: 单实例 Mutex 锁 + 全局热键管理器 (RegisterHotKey + PeekMessage 泵) +
  SendInput Unicode 注入 + 剪贴板文本/CF_DIB 图像读写 + AttachThreadInput 焦点切换
- internal/hotkey: "Ctrl+Alt+G" 风格快捷键字符串解析/格式化
- internal/qr: gozxing 二维码解码 + otpauth:// URI 解析
- internal/i18n: en/zh-CN/de 三语 TOML

UI 模块 (Gio):
- 主窗口:圆环倒计时进度条、复制按钮 + Toast 反馈、空列表占位、行分隔线
- 添加流程:vendor 菜单 + 各 vendor 独立对话框 + 二维码扫描入口(文件 / 剪贴板)
- 设置:密码加密、老版 XML 导入、每条目热键配置
- Steam:注册向导(含 captcha/email/SMS 多步)+ 交易确认窗

构建:Windows 主目标,非 Windows 平台所有 Win32 功能走 build-tag 桩实现。
2026-06-12 03:10:37 +08:00

143 lines
3.6 KiB
Go

// Package hotkey converts between human-readable hotkey strings
// ("Ctrl+Alt+G") and the win32.Hotkey struct the registration syscall
// expects. The mapping is intentionally limited to the keys WinAuth
// users actually configured: letters, digits, F1-F12.
package hotkey
import (
"errors"
"fmt"
"strings"
"git.wxccs.org/iceking2nd/winauth-go/internal/win32"
)
// ErrEmpty is returned by Parse when the input is empty or whitespace.
// It is *not* a syntax error — callers typically treat it as "no
// hotkey configured" rather than a validation failure.
var ErrEmpty = errors.New("hotkey: empty")
// Parse turns "Ctrl+Alt+G" / "ctrl + shift + f5" into a win32.Hotkey.
// Whitespace and case are ignored. Modifier order is irrelevant.
func Parse(s string) (win32.Hotkey, error) {
s = strings.TrimSpace(s)
if s == "" {
return win32.Hotkey{}, ErrEmpty
}
parts := strings.Split(s, "+")
if len(parts) == 0 {
return win32.Hotkey{}, fmt.Errorf("hotkey: malformed %q", s)
}
var mods uint32
var key string
for _, p := range parts {
token := strings.ToLower(strings.TrimSpace(p))
switch token {
case "ctrl", "control":
mods |= win32.ModCtrl
case "alt":
mods |= win32.ModAlt
case "shift":
mods |= win32.ModShift
case "win", "super":
mods |= win32.ModWin
case "":
// tolerate trailing "+"
default:
if key != "" {
return win32.Hotkey{}, fmt.Errorf("hotkey: more than one base key in %q", s)
}
key = token
}
}
if key == "" {
return win32.Hotkey{}, fmt.Errorf("hotkey: no base key in %q", s)
}
if mods == 0 {
return win32.Hotkey{}, fmt.Errorf("hotkey: %q has no modifier (would conflict with normal typing)", s)
}
vk, ok := vkFromName(key)
if !ok {
return win32.Hotkey{}, fmt.Errorf("hotkey: unsupported key %q", key)
}
return win32.Hotkey{Mods: mods | win32.ModNoRepeat, VK: vk}, nil
}
// Format canonicalises h back into a "Ctrl+Alt+G" style string. The
// modifier order is fixed (Ctrl, Alt, Shift, Win) so two equivalent
// hotkeys render identically.
func Format(h win32.Hotkey) string {
if h.VK == 0 {
return ""
}
parts := make([]string, 0, 4)
if h.Mods&win32.ModCtrl != 0 {
parts = append(parts, "Ctrl")
}
if h.Mods&win32.ModAlt != 0 {
parts = append(parts, "Alt")
}
if h.Mods&win32.ModShift != 0 {
parts = append(parts, "Shift")
}
if h.Mods&win32.ModWin != 0 {
parts = append(parts, "Win")
}
parts = append(parts, nameFromVK(h.VK))
return strings.Join(parts, "+")
}
// vkFromName maps the lowercase key name to a Win32 virtual-key code.
// Returns false for anything it doesn't know.
func vkFromName(name string) (uint32, bool) {
if len(name) == 1 {
c := name[0]
switch {
case c >= 'a' && c <= 'z':
return uint32(c - 'a' + 'A'), true
case c >= '0' && c <= '9':
return uint32(c), true
}
}
if strings.HasPrefix(name, "f") {
// F1=0x70, F12=0x7B
var n int
if _, err := fmt.Sscanf(name, "f%d", &n); err == nil && n >= 1 && n <= 12 {
return uint32(0x70 + n - 1), true
}
}
switch name {
case "space":
return 0x20, true
case "enter", "return":
return 0x0D, true
case "tab":
return 0x09, true
}
return 0, false
}
// nameFromVK is the inverse of vkFromName for the cases Parse accepts.
// Unknown codes render as their hex value so the UI still shows
// something.
func nameFromVK(vk uint32) string {
switch {
case vk >= 'A' && vk <= 'Z':
return string(rune(vk))
case vk >= '0' && vk <= '9':
return string(rune(vk))
case vk >= 0x70 && vk <= 0x7B:
return fmt.Sprintf("F%d", vk-0x70+1)
}
switch vk {
case 0x20:
return "Space"
case 0x0D:
return "Enter"
case 0x09:
return "Tab"
}
return fmt.Sprintf("0x%X", vk)
}