You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
win32api/user32.go

167 lines
4.3 KiB
Go

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)
}