func SendString(keys string) { cs := strings.Split(keys, "") for i := 0; i < len(cs); i++ { c := cs[i] cc := *syscall.StringToUTF16Ptr(c) cr := []rune(c)[0] //cc := uint16(cr) fmt.Printf("KEY c=%v cc=%v\n", c, cc) if cc >= 0 && cc < 256 { vk := win.VkKeyScan(cc) //vk, _ := getVKey(uint32(cc)) fmt.Printf("vk=%v\n", vk) if vk == -1 { UniKeyPress(cc) } else { if vk < 0 { vk = ^vk + 0x1 } fmt.Printf("vk=%v\n", vk) shift := (vk>>8&0x1 == 0x1) ks := win.GetKeyState(win.VK_CAPITAL) fmt.Printf("shift=%v ww=%v\n", shift, ks) if win.GetKeyState(win.VK_CAPITAL)&0x1 == 0x0 { if (cr >= 'a' && cr <= 'z') || (cr >= 'A' && cr <= 'Z') { //shift = !shift } } KeyPress(uint16(vk&0xFF), shift) } } } }
// 模拟按键 func InjectKey(h *cef.BrowserHost, key string) { if len(key) != 1 { fmt.Printf("Argement error.") return } var event cef.CefKeyEvent var scanKey uint16 var vkCode byte var scanCode uint32 scanKey = *syscall.StringToUTF16Ptr(key) vkCode = win.LOBYTE(uint16(win.VkKeyScan(scanKey))) scanCode = win.MapVirtualKey(uint32(vkCode), win.MAPVK_VK_TO_VSC) //fmt.Printf("vkCode=%v, scanCode=%v\n", VkCode, scanCode) event.IsSystemKey = false event.Modifiers = 0 event.NativeKeyCode = int((scanCode << 16) | // key scan code 1) // key repeat count event.WindowsKeyCode = int(vkCode) event.Type = cef.KEYEVENT_RAWKEYDOWN h.SendKeyEvent(&event) event.WindowsKeyCode = int(scanKey) event.Type = cef.KEYEVENT_CHAR h.SendKeyEvent(&event) event.WindowsKeyCode = int(vkCode) // bits 30 and 31 should be always 1 for WM_KEYUP event.NativeKeyCode = int(uint32(event.NativeKeyCode) | 0xC0000000) event.Type = cef.KEYEVENT_KEYUP h.SendKeyEvent(&event) }