package win32api

import (
	"errors"
	"syscall"
)

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
}
