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.
305 lines
8.1 KiB
Go
305 lines
8.1 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)
|
|
}
|
|
|
|
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() ([]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 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
|
|
}
|