package win32api import ( "errors" "syscall" "unsafe" ) func Keybd_event(keyname string, keydown bool) error { var key int var down uintptr if !keydown { down = KEYEVENTF_KEYUP } switch keyname { case "shift": key = VK_SHIFT case "lshift": key = VK_LSHIFT case "rshift": key = VK_RSHIFT case "a": key = VK_A } user32, err := syscall.LoadLibrary("user32.dll") if err != nil { return errors.New("Can't Load User32 API") } defer syscall.FreeLibrary(user32) keyevent, err := syscall.GetProcAddress(syscall.Handle(user32), "keybd_event") if err != nil { return errors.New("Can't Load Keybd_event API") } syscall.Syscall6(keyevent, 4, uintptr(key), uintptr(key), down, 0, 0, 0) return nil } func Keybd_event_origin(key, keyenv, down, extra uintptr) error { user32, err := syscall.LoadLibrary("user32.dll") if err != nil { return errors.New("Can't Load User32 API") } defer syscall.FreeLibrary(user32) keyevent, err := syscall.GetProcAddress(syscall.Handle(user32), "keybd_event") if err != nil { return errors.New("Can't Load Keybd_event API") } syscall.Syscall6(keyevent, 4, key, keyenv, down, extra, 0, 0) return nil } func OpenClipboard(hWnd HWND) error { user32, err := syscall.LoadLibrary("user32.dll") if err != nil { return errors.New("Can't Load User32 API") } defer syscall.FreeLibrary(user32) oc, err := syscall.GetProcAddress(syscall.Handle(user32), "OpenClipboard") if err != nil { return errors.New("Can't Load OpenClipboard API") } if hWnd != 0 { if r, _, errno := syscall.Syscall(oc, 1, uintptr(hWnd), 0, 0); r == 0 { return error(errno) } return nil } if r, _, errno := syscall.Syscall(oc, 0, 0, 0, 0); r == 0 { return error(errno) } return nil } func CloseClipboard() error { user32, err := syscall.LoadLibrary("user32.dll") if err != nil { return errors.New("Can't Load User32 API") } defer syscall.FreeLibrary(user32) cc, err := syscall.GetProcAddress(syscall.Handle(user32), "CloseClipboard") if err != nil { return errors.New("Can't Load CloseClipboard API") } if r, _, errno := syscall.Syscall(cc, 0, 0, 0, 0); r == 0 { return error(errno) } return nil } func GetClipboardData(uFormat DWORD) (HGLOBAL, error) { user32, err := syscall.LoadLibrary("user32.dll") if err != nil { return 0, errors.New("Can't Load User32 API") } defer syscall.FreeLibrary(user32) gcd, err := syscall.GetProcAddress(syscall.Handle(user32), "GetClipboardData") if err != nil { return 0, errors.New("Can't Load GetClipboardData API") } r, _, errno := syscall.Syscall(gcd, 1, uintptr(uFormat), 0, 0) if r == 0 { return 0, error(errno) } return HGLOBAL(r), nil } func EnumClipboardFormats(uFormat DWORD) (DWORD, error) { user32, err := syscall.LoadLibrary("user32.dll") if err != nil { return 0, errors.New("Can't Load User32 API") } defer syscall.FreeLibrary(user32) ecf, err := syscall.GetProcAddress(syscall.Handle(user32), "EnumClipboardFormats") if err != nil { return 0, errors.New("Can't Load EnumClipboardFormats API") } r, _, errno := syscall.Syscall(ecf, 1, uintptr(uFormat), 0, 0) if r == 0 { return 0, error(errno) } return DWORD(r), nil } func EnumAllClipboardFormats() ([]DWORD, error) { var formats []DWORD for i := 0; ; i++ { format, err := EnumClipboardFormats(DWORD(i)) if err != nil { break } formats = append(formats, format) } return formats, nil } func GetClipboardFormatName(uFormat DWORD) (string, error) { user32, err := syscall.LoadLibrary("user32.dll") if err != nil { return "", errors.New("Can't Load User32 API") } defer syscall.FreeLibrary(user32) gcfn, err := syscall.GetProcAddress(syscall.Handle(user32), "GetClipboardFormatNameW") if err != nil { return "", errors.New("Can't Load GetClipboardFormatName API") } var buffer [256]uint16 r, _, errno := syscall.Syscall6(gcfn, 3, uintptr(uFormat), uintptr(unsafe.Pointer(&buffer)), uintptr(len(buffer)), 0, 0, 0) if r == 0 { return "", error(errno) } return syscall.UTF16ToString(buffer[:]), nil } func RegisterClipboardFormat(lpszFormat string) DWORD { user32, err := syscall.LoadLibrary("user32.dll") if err != nil { return 0 } defer syscall.FreeLibrary(user32) rcf, err := syscall.GetProcAddress(syscall.Handle(user32), "RegisterClipboardFormatW") if err != nil { return 0 } r, _, _ := syscall.Syscall(rcf, 1, uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(lpszFormat))), 0, 0) return DWORD(r) } func EmptyClipboard() error { user32, err := syscall.LoadLibrary("user32.dll") if err != nil { return errors.New("Can't Load User32 API") } defer syscall.FreeLibrary(user32) ec, err := syscall.GetProcAddress(syscall.Handle(user32), "EmptyClipboard") if err != nil { return errors.New("Can't Load EmptyClipboard API") } if r, _, errno := syscall.Syscall(ec, 0, 0, 0, 0); r == 0 { return error(errno) } return nil } func CountClipboardFormats() (int, error) { user32, err := syscall.LoadLibrary("user32.dll") if err != nil { return 0, errors.New("Can't Load User32 API") } defer syscall.FreeLibrary(user32) ccf, err := syscall.GetProcAddress(syscall.Handle(user32), "CountClipboardFormats") if err != nil { return 0, errors.New("Can't Load CountClipboardFormats API") } r, _, errno := syscall.Syscall(ccf, 0, 0, 0, 0) if r == 0 { return 0, error(errno) } return int(r), nil } func GetClipboardOwner() (HWND, error) { user32, err := syscall.LoadLibrary("user32.dll") if err != nil { return 0, errors.New("Can't Load User32 API") } defer syscall.FreeLibrary(user32) gco, err := syscall.GetProcAddress(syscall.Handle(user32), "GetClipboardOwner") if err != nil { return 0, errors.New("Can't Load GetClipboardOwner API") } r, _, errno := syscall.Syscall(gco, 0, 0, 0, 0) if r == 0 { return 0, error(errno) } return HWND(r), nil } func GetUpdatedClipboardFormats(lpuiFormats unsafe.Pointer, cFormats int, pcFormats unsafe.Pointer) (int, error) { user32, err := syscall.LoadLibrary("user32.dll") if err != nil { return 0, errors.New("Can't Load User32 API") } defer syscall.FreeLibrary(user32) gucf, err := syscall.GetProcAddress(syscall.Handle(user32), "GetUpdatedClipboardFormats") if err != nil { return 0, errors.New("Can't Load GetUpdatedClipboardFormats API") } r, _, errno := syscall.Syscall(gucf, 3, uintptr(lpuiFormats), uintptr(cFormats), uintptr(pcFormats)) if r == 0 { return 0, error(errno) } return int(r), nil } func GetUpdatedClipboardFormatsAll() ([]DWORD, error) { var res []DWORD formats := make([]uint32, 32) var count uint32 _, err := GetUpdatedClipboardFormats(unsafe.Pointer(&formats[0]), len(formats), unsafe.Pointer(&count)) if err != nil { return nil, err } for i := 0; i < int(count); i++ { res = append(res, DWORD(formats[i])) } return res, err } func IsClipboardFormatAvailable(uFormat DWORD) (bool, error) { user32, err := syscall.LoadLibrary("user32.dll") if err != nil { return false, err } defer syscall.FreeLibrary(user32) icfa, err := syscall.GetProcAddress(syscall.Handle(user32), "IsClipboardFormatAvailable") if err != nil { return false, err } r, _, _ := syscall.Syscall(icfa, 1, uintptr(uFormat), 0, 0) return r != 0, nil } func AddClipboardFormatListener(hWnd HWND) (bool, error) { user32, err := syscall.LoadLibrary("user32.dll") if err != nil { return false, err } defer syscall.FreeLibrary(user32) acfl, err := syscall.GetProcAddress(syscall.Handle(user32), "AddClipboardFormatListener") if err != nil { return false, err } r, _, _ := syscall.Syscall(acfl, 1, uintptr(hWnd), 0, 0) return r != 0, nil } func RemoveClipboardFormatListener(hWnd HWND) (bool, error) { user32, err := syscall.LoadLibrary("user32.dll") if err != nil { return false, err } defer syscall.FreeLibrary(user32) rcfl, err := syscall.GetProcAddress(syscall.Handle(user32), "RemoveClipboardFormatListener") if err != nil { return false, err } r, _, _ := syscall.Syscall(rcfl, 1, uintptr(hWnd), 0, 0) return r != 0, nil } func SetClipboardData(uFormat DWORD, hMem HGLOBAL) (HGLOBAL, error) { user32, err := syscall.LoadLibrary("user32.dll") if err != nil { return 0, errors.New("Can't Load User32 API") } defer syscall.FreeLibrary(user32) scd, err := syscall.GetProcAddress(syscall.Handle(user32), "SetClipboardData") if err != nil { return 0, errors.New("Can't Load SetClipboardData API") } r, _, errno := syscall.Syscall(scd, 2, uintptr(uFormat), uintptr(hMem), 0) if r == 0 { return 0, error(errno) } return HGLOBAL(r), nil } func SetClipboardViewer(hWndNewViewer HWND) (HWND, error) { user32, err := syscall.LoadLibrary("user32.dll") if err != nil { return 0, errors.New("Can't Load User32 API") } defer syscall.FreeLibrary(user32) scv, err := syscall.GetProcAddress(syscall.Handle(user32), "SetClipboardViewer") if err != nil { return 0, errors.New("Can't Load SetClipboardViewer API") } r, _, errno := syscall.Syscall(scv, 1, uintptr(hWndNewViewer), 0, 0) if r == 0 { return 0, error(errno) } return HWND(r), nil } func CreateWindowEx(dwExStyle DWORD, lpClassName, lpWindowName string, dwStyle DWORD, x, y, nWidth, nHeight int, hWndParent HWND, hMenu HMENU, hInstance HINSTANCE, lpParam unsafe.Pointer) (HWND, error) { user32, err := syscall.LoadLibrary("user32.dll") if err != nil { return 0, errors.New("Can't Load User32 API") } defer syscall.FreeLibrary(user32) cwe, err := syscall.GetProcAddress(syscall.Handle(user32), "CreateWindowExW") if err != nil { return 0, errors.New("Can't Load CreateWindowEx API") } r, _, errno := syscall.Syscall12(cwe, 11, uintptr(dwExStyle), uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(lpClassName))), uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(lpWindowName))), uintptr(dwStyle), uintptr(x), uintptr(y), uintptr(nWidth), uintptr(nHeight), uintptr(hWndParent), uintptr(hMenu), uintptr(hInstance), uintptr(lpParam)) if r == 0 { return 0, error(errno) } return HWND(r), nil } func GetMessage(lpMsg *MSG, hWnd HWND, wMsgFilterMin, wMsgFilterMax DWORD) (DWORD, error) { user32, err := syscall.LoadLibrary("user32.dll") if err != nil { return 0, errors.New("Can't Load User32 API") } defer syscall.FreeLibrary(user32) gm, err := syscall.GetProcAddress(syscall.Handle(user32), "GetMessageW") if err != nil { return 0, errors.New("Can't Load GetMessage API") } r, _, errno := syscall.Syscall6(gm, 4, uintptr(unsafe.Pointer(lpMsg)), uintptr(hWnd), uintptr(wMsgFilterMin), uintptr(wMsgFilterMax), 0, 0) if r == 0 { return 0, error(errno) } return DWORD(r), nil }