-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathreplacer_windows.go
More file actions
119 lines (98 loc) · 3.17 KB
/
Copy pathreplacer_windows.go
File metadata and controls
119 lines (98 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
//go:build windows
package main
import (
"sync/atomic"
"time"
"unsafe"
)
var (
procSendInput = user32.NewProc("SendInput")
procActivateKeyboardLayout = user32.NewProc("ActivateKeyboardLayout")
procGetKeyboardLayoutList = user32.NewProc("GetKeyboardLayoutList")
procPostMessage = user32.NewProc("PostMessageW")
)
const (
INPUT_KEYBOARD = 1
KEYEVENTF_KEYUP = 0x0002
KEYEVENTF_UNICODE = 0x0004
WM_INPUTLANGCHANGEREQUEST = 0x0050
HWND_BROADCAST = 0xFFFF
HKL_NEXT = 1
)
type INPUT struct {
Type uint32
Ki KEYBDINPUT
_ [8]byte // padding
}
type KEYBDINPUT struct {
WVk uint16
WScan uint16
DwFlags uint32
Time uint32
DwExtraInfo uintptr
}
var replacing int32
func replaceText(buf *Buffer, deleteChars int, newText string) {
atomic.StoreInt32(&replacing, 1)
buf.Clear()
// Send backspaces
for i := 0; i < deleteChars; i++ {
sendKey(VK_BACK, 0)
time.Sleep(5 * time.Millisecond)
}
time.Sleep(10 * time.Millisecond)
// Send corrected text as unicode
for _, ch := range newText {
sendUnicode(ch)
time.Sleep(5 * time.Millisecond)
}
// Switch keyboard layout
switchLayoutWindows()
time.Sleep(30 * time.Millisecond)
atomic.StoreInt32(&replacing, 0)
}
func sendKey(vk uint16, flags uint32) {
inputs := [2]INPUT{
{Type: INPUT_KEYBOARD, Ki: KEYBDINPUT{WVk: vk, DwFlags: flags}},
{Type: INPUT_KEYBOARD, Ki: KEYBDINPUT{WVk: vk, DwFlags: flags | KEYEVENTF_KEYUP}},
}
procSendInput.Call(2, uintptr(unsafe.Pointer(&inputs[0])), unsafe.Sizeof(inputs[0]))
}
func sendUnicode(ch rune) {
inputs := [2]INPUT{
{Type: INPUT_KEYBOARD, Ki: KEYBDINPUT{WScan: uint16(ch), DwFlags: KEYEVENTF_UNICODE}},
{Type: INPUT_KEYBOARD, Ki: KEYBDINPUT{WScan: uint16(ch), DwFlags: KEYEVENTF_UNICODE | KEYEVENTF_KEYUP}},
}
procSendInput.Call(2, uintptr(unsafe.Pointer(&inputs[0])), unsafe.Sizeof(inputs[0]))
}
func switchLayoutWindows() {
procActivateKeyboardLayout.Call(HKL_NEXT, 0)
}
// Go wrappers to match macOS API — used by main.go
func sendBackspaceKey() { sendKey(VK_BACK, 0) }
func sendChar(ch rune) { sendUnicode(ch) }
func switchLang() { switchLayoutWindows() }
func sendEnter() { sendKey(VK_RETURN, 0) }
// IsRussianLayout — stub for Windows (layout detection via GetKeyboardLayout)
func IsRussianLayout() bool {
hwnd, _, _ := procGetForegroundWindow.Call()
tid, _, _ := procGetWindowThreadProcessId.Call(hwnd, 0)
hkl, _, _ := procGetKeyboardLayout.Call(tid)
// Russian LCID = 0x0419
return (hkl & 0xFFFF) == 0x0419
}
// FrontmostAppID — stub for Windows (returns empty for now)
func FrontmostAppID() string {
return ""
}
// Tray stubs for Windows (TODO: implement Win32 tray icon)
func startTray() {}
func runAppLoop() { select {} }
func isTrayEnabled() bool { return true }
// installFrontmostObserver — no-op on Windows (FrontmostAppID uses direct call)
func installFrontmostObserver() {}
// Clipboard stubs for Windows (TODO: implement via OpenClipboard/SetClipboardData)
func readClipboard() string { return "" }
func writeClipboard(s string) {}
func sendCopy() { /* TODO: send Ctrl+C via SendInput */ }
func sendPaste() { /* TODO: send Ctrl+V via SendInput */ }