var (
user32 = syscall.NewLazyDLL("user32.dll")
procGetKeyboardLayoutName = user32.NewProc("GetKeyboardLayoutNameA")
procGetKeyboardLayout = user32.NewProc("GetKeyboardLayout")
procGetForegroundWindow = user32.NewProc("GetForegroundWindow")
procGetWindowThreadProcessID = user32.NewProc("GetWindowThreadProcessId")
)
func GetForegroundWindow() (hwnd syscall.Handle, err error) {
r0, _, e1 := syscall.Syscall(procGetForegroundWindow.Addr(), 0, 0, 0, 0)
if e1 != 0 {
err = error(e1)
return
}
hwnd = syscall.Handle(r0)
return
}
func GetWindowThreadProcessID(hwnd syscall.Handle) (uintptr, int) {
var processID int
ret, _, _ := procGetWindowThreadProcessID.Call(
uintptr(hwnd),
uintptr(unsafe.Pointer(&processID)))
return uintptr(ret), processID
}
func main() {
for {
var str *string
n, _, err := procGetKeyboardLayoutName.Call(uintptr(unsafe.Pointer(&str)))
fmt.Println(n, str, err)
Handle, _ := GetForegroundWindow()
_, processID := GetWindowThreadProcessID(Handle)
hklLayout, _, err2 := procGetKeyboardLayout.Call(uintptr(processID))
fmt.Println(hklLayout, processID, err2)
hklLayout, _, err2 = procGetKeyboardLayout.Call(uintptr(0))
fmt.Println(hklLayout, err2)
time.Sleep(5000 * time.Millisecond)
}
}