key.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. // Copyright 2016 The go-vgo Project Developers. See the COPYRIGHT
  2. // file at the top-level directory of this distribution and at
  3. // https://github.com/go-vgo/robotgo/blob/master/LICENSE
  4. //
  5. // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
  6. // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
  7. // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
  8. // option. This file may not be copied, modified, or distributed
  9. // except according to those terms.
  10. package robotgo
  11. /*
  12. // #include "key/keycode.h"
  13. #include "key/keypress_c.h"
  14. */
  15. import "C"
  16. import (
  17. "errors"
  18. "math/rand"
  19. "reflect"
  20. "runtime"
  21. "strconv"
  22. "strings"
  23. "unicode"
  24. "unsafe"
  25. "github.com/go-vgo/robotgo/clipboard"
  26. )
  27. // Defining a bunch of constants.
  28. const (
  29. // KeyA define key "a"
  30. KeyA = "a"
  31. KeyB = "b"
  32. KeyC = "c"
  33. KeyD = "d"
  34. KeyE = "e"
  35. KeyF = "f"
  36. KeyG = "g"
  37. KeyH = "h"
  38. KeyI = "i"
  39. KeyJ = "j"
  40. KeyK = "k"
  41. KeyL = "l"
  42. KeyM = "m"
  43. KeyN = "n"
  44. KeyO = "o"
  45. KeyP = "p"
  46. KeyQ = "q"
  47. KeyR = "r"
  48. KeyS = "s"
  49. KeyT = "t"
  50. KeyU = "u"
  51. KeyV = "v"
  52. KeyW = "w"
  53. KeyX = "x"
  54. KeyY = "y"
  55. KeyZ = "z"
  56. //
  57. CapA = "A"
  58. CapB = "B"
  59. CapC = "C"
  60. CapD = "D"
  61. CapE = "E"
  62. CapF = "F"
  63. CapG = "G"
  64. CapH = "H"
  65. CapI = "I"
  66. CapJ = "J"
  67. CapK = "K"
  68. CapL = "L"
  69. CapM = "M"
  70. CapN = "N"
  71. CapO = "O"
  72. CapP = "P"
  73. CapQ = "Q"
  74. CapR = "R"
  75. CapS = "S"
  76. CapT = "T"
  77. CapU = "U"
  78. CapV = "V"
  79. CapW = "W"
  80. CapX = "X"
  81. CapY = "Y"
  82. CapZ = "Z"
  83. //
  84. Key0 = "0"
  85. Key1 = "1"
  86. Key2 = "2"
  87. Key3 = "3"
  88. Key4 = "4"
  89. Key5 = "5"
  90. Key6 = "6"
  91. Key7 = "7"
  92. Key8 = "8"
  93. Key9 = "9"
  94. // Backspace backspace key string
  95. Backspace = "backspace"
  96. Delete = "delete"
  97. Enter = "enter"
  98. Tab = "tab"
  99. Esc = "esc"
  100. Escape = "escape"
  101. Up = "up" // Up arrow key
  102. Down = "down" // Down arrow key
  103. Right = "right" // Right arrow key
  104. Left = "left" // Left arrow key
  105. Home = "home"
  106. End = "end"
  107. Pageup = "pageup"
  108. Pagedown = "pagedown"
  109. F1 = "f1"
  110. F2 = "f2"
  111. F3 = "f3"
  112. F4 = "f4"
  113. F5 = "f5"
  114. F6 = "f6"
  115. F7 = "f7"
  116. F8 = "f8"
  117. F9 = "f9"
  118. F10 = "f10"
  119. F11 = "f11"
  120. F12 = "f12"
  121. F13 = "f13"
  122. F14 = "f14"
  123. F15 = "f15"
  124. F16 = "f16"
  125. F17 = "f17"
  126. F18 = "f18"
  127. F19 = "f19"
  128. F20 = "f20"
  129. F21 = "f21"
  130. F22 = "f22"
  131. F23 = "f23"
  132. F24 = "f24"
  133. Cmd = "cmd" // is the "win" key for windows
  134. Lcmd = "lcmd" // left command
  135. Rcmd = "rcmd" // right command
  136. // "command"
  137. Alt = "alt"
  138. Lalt = "lalt" // left alt
  139. Ralt = "ralt" // right alt
  140. Ctrl = "ctrl"
  141. Lctrl = "lctrl" // left ctrl
  142. Rctrl = "rctrl" // right ctrl
  143. Control = "control"
  144. Shift = "shift"
  145. Lshift = "lshift" // left shift
  146. Rshift = "rshift" // right shift
  147. // "right_shift"
  148. Capslock = "capslock"
  149. Space = "space"
  150. Print = "print"
  151. Printscreen = "printscreen" // No Mac support
  152. Insert = "insert"
  153. Menu = "menu" // Windows only
  154. AudioMute = "audio_mute" // Mute the volume
  155. AudioVolDown = "audio_vol_down" // Lower the volume
  156. AudioVolUp = "audio_vol_up" // Increase the volume
  157. AudioPlay = "audio_play"
  158. AudioStop = "audio_stop"
  159. AudioPause = "audio_pause"
  160. AudioPrev = "audio_prev" // Previous Track
  161. AudioNext = "audio_next" // Next Track
  162. AudioRewind = "audio_rewind" // Linux only
  163. AudioForward = "audio_forward" // Linux only
  164. AudioRepeat = "audio_repeat" // Linux only
  165. AudioRandom = "audio_random" // Linux only
  166. Num0 = "num0" // numpad 0
  167. Num1 = "num1"
  168. Num2 = "num2"
  169. Num3 = "num3"
  170. Num4 = "num4"
  171. Num5 = "num5"
  172. Num6 = "num6"
  173. Num7 = "num7"
  174. Num8 = "num8"
  175. Num9 = "num9"
  176. NumLock = "num_lock"
  177. NumDecimal = "num."
  178. NumPlus = "num+"
  179. NumMinus = "num-"
  180. NumMul = "num*"
  181. NumDiv = "num/"
  182. NumClear = "num_clear"
  183. NumEnter = "num_enter"
  184. NumEqual = "num_equal"
  185. LightsMonUp = "lights_mon_up" // Turn up monitor brightness No Windows support
  186. LightsMonDown = "lights_mon_down" // Turn down monitor brightness No Windows support
  187. LightsKbdToggle = "lights_kbd_toggle" // Toggle keyboard backlight on/off No Windows support
  188. LightsKbdUp = "lights_kbd_up" // Turn up keyboard backlight brightness No Windows support
  189. LightsKbdDown = "lights_kbd_down"
  190. )
  191. // keyNames define a map of key names to MMKeyCode
  192. var keyNames = map[string]C.MMKeyCode{
  193. "backspace": C.K_BACKSPACE,
  194. "delete": C.K_DELETE,
  195. "enter": C.K_RETURN,
  196. "tab": C.K_TAB,
  197. "esc": C.K_ESCAPE,
  198. "escape": C.K_ESCAPE,
  199. "up": C.K_UP,
  200. "down": C.K_DOWN,
  201. "right": C.K_RIGHT,
  202. "left": C.K_LEFT,
  203. "home": C.K_HOME,
  204. "end": C.K_END,
  205. "pageup": C.K_PAGEUP,
  206. "pagedown": C.K_PAGEDOWN,
  207. //
  208. "f1": C.K_F1,
  209. "f2": C.K_F2,
  210. "f3": C.K_F3,
  211. "f4": C.K_F4,
  212. "f5": C.K_F5,
  213. "f6": C.K_F6,
  214. "f7": C.K_F7,
  215. "f8": C.K_F8,
  216. "f9": C.K_F9,
  217. "f10": C.K_F10,
  218. "f11": C.K_F11,
  219. "f12": C.K_F12,
  220. "f13": C.K_F13,
  221. "f14": C.K_F14,
  222. "f15": C.K_F15,
  223. "f16": C.K_F16,
  224. "f17": C.K_F17,
  225. "f18": C.K_F18,
  226. "f19": C.K_F19,
  227. "f20": C.K_F20,
  228. "f21": C.K_F21,
  229. "f22": C.K_F22,
  230. "f23": C.K_F23,
  231. "f24": C.K_F24,
  232. //
  233. "cmd": C.K_META,
  234. "lcmd": C.K_LMETA,
  235. "rcmd": C.K_RMETA,
  236. "command": C.K_META,
  237. "alt": C.K_ALT,
  238. "lalt": C.K_LALT,
  239. "ralt": C.K_RALT,
  240. "ctrl": C.K_CONTROL,
  241. "lctrl": C.K_LCONTROL,
  242. "rctrl": C.K_RCONTROL,
  243. "control": C.K_CONTROL,
  244. "shift": C.K_SHIFT,
  245. "lshift": C.K_LSHIFT,
  246. "rshift": C.K_RSHIFT,
  247. "right_shift": C.K_RSHIFT,
  248. "capslock": C.K_CAPSLOCK,
  249. "space": C.K_SPACE,
  250. "print": C.K_PRINTSCREEN,
  251. "printscreen": C.K_PRINTSCREEN,
  252. "insert": C.K_INSERT,
  253. "menu": C.K_MENU,
  254. "audio_mute": C.K_AUDIO_VOLUME_MUTE,
  255. "audio_vol_down": C.K_AUDIO_VOLUME_DOWN,
  256. "audio_vol_up": C.K_AUDIO_VOLUME_UP,
  257. "audio_play": C.K_AUDIO_PLAY,
  258. "audio_stop": C.K_AUDIO_STOP,
  259. "audio_pause": C.K_AUDIO_PAUSE,
  260. "audio_prev": C.K_AUDIO_PREV,
  261. "audio_next": C.K_AUDIO_NEXT,
  262. "audio_rewind": C.K_AUDIO_REWIND,
  263. "audio_forward": C.K_AUDIO_FORWARD,
  264. "audio_repeat": C.K_AUDIO_REPEAT,
  265. "audio_random": C.K_AUDIO_RANDOM,
  266. "num0": C.K_NUMPAD_0,
  267. "num1": C.K_NUMPAD_1,
  268. "num2": C.K_NUMPAD_2,
  269. "num3": C.K_NUMPAD_3,
  270. "num4": C.K_NUMPAD_4,
  271. "num5": C.K_NUMPAD_5,
  272. "num6": C.K_NUMPAD_6,
  273. "num7": C.K_NUMPAD_7,
  274. "num8": C.K_NUMPAD_8,
  275. "num9": C.K_NUMPAD_9,
  276. "num_lock": C.K_NUMPAD_LOCK,
  277. // todo: removed
  278. "numpad_0": C.K_NUMPAD_0,
  279. "numpad_1": C.K_NUMPAD_1,
  280. "numpad_2": C.K_NUMPAD_2,
  281. "numpad_3": C.K_NUMPAD_3,
  282. "numpad_4": C.K_NUMPAD_4,
  283. "numpad_5": C.K_NUMPAD_5,
  284. "numpad_6": C.K_NUMPAD_6,
  285. "numpad_7": C.K_NUMPAD_7,
  286. "numpad_8": C.K_NUMPAD_8,
  287. "numpad_9": C.K_NUMPAD_9,
  288. "numpad_lock": C.K_NUMPAD_LOCK,
  289. "num.": C.K_NUMPAD_DECIMAL,
  290. "num+": C.K_NUMPAD_PLUS,
  291. "num-": C.K_NUMPAD_MINUS,
  292. "num*": C.K_NUMPAD_MUL,
  293. "num/": C.K_NUMPAD_DIV,
  294. "num_clear": C.K_NUMPAD_CLEAR,
  295. "num_enter": C.K_NUMPAD_ENTER,
  296. "num_equal": C.K_NUMPAD_EQUAL,
  297. "lights_mon_up": C.K_LIGHTS_MON_UP,
  298. "lights_mon_down": C.K_LIGHTS_MON_DOWN,
  299. "lights_kbd_toggle": C.K_LIGHTS_KBD_TOGGLE,
  300. "lights_kbd_up": C.K_LIGHTS_KBD_UP,
  301. "lights_kbd_down": C.K_LIGHTS_KBD_DOWN,
  302. // { NULL: C.K_NOT_A_KEY }
  303. }
  304. // CmdCtrl If the operating system is macOS, return the key string "cmd",
  305. // otherwise return the key string "ctrl
  306. func CmdCtrl() string {
  307. if runtime.GOOS == "darwin" {
  308. return "cmd"
  309. }
  310. return "ctrl"
  311. }
  312. // It sends a key press and release to the active application
  313. func tapKeyCode(code C.MMKeyCode, flags C.MMKeyFlags, pid C.uintptr) {
  314. C.toggleKeyCode(code, true, flags, pid)
  315. MilliSleep(3)
  316. C.toggleKeyCode(code, false, flags, pid)
  317. }
  318. var keyErr = errors.New("Invalid key flag specified.")
  319. func checkKeyCodes(k string) (key C.MMKeyCode, err error) {
  320. if k == "" {
  321. return
  322. }
  323. if len(k) == 1 {
  324. val1 := C.CString(k)
  325. defer C.free(unsafe.Pointer(val1))
  326. key = C.keyCodeForChar(*val1)
  327. if key == C.K_NOT_A_KEY {
  328. err = keyErr
  329. return
  330. }
  331. return
  332. }
  333. if v, ok := keyNames[k]; ok {
  334. key = v
  335. if key == C.K_NOT_A_KEY {
  336. err = keyErr
  337. return
  338. }
  339. }
  340. return
  341. }
  342. func checkKeyFlags(f string) (flags C.MMKeyFlags) {
  343. m := map[string]C.MMKeyFlags{
  344. "alt": C.MOD_ALT,
  345. "ralt": C.MOD_ALT,
  346. "lalt": C.MOD_ALT,
  347. "cmd": C.MOD_META,
  348. "rcmd": C.MOD_META,
  349. "lcmd": C.MOD_META,
  350. "ctrl": C.MOD_CONTROL,
  351. "rctrl": C.MOD_CONTROL,
  352. "lctrl": C.MOD_CONTROL,
  353. "shift": C.MOD_SHIFT,
  354. "rshift": C.MOD_SHIFT,
  355. "lshift": C.MOD_SHIFT,
  356. "none": C.MOD_NONE,
  357. }
  358. if v, ok := m[f]; ok {
  359. return v
  360. }
  361. return
  362. }
  363. func getFlagsFromValue(value []string) (flags C.MMKeyFlags) {
  364. if len(value) <= 0 {
  365. return
  366. }
  367. for i := 0; i < len(value); i++ {
  368. var f C.MMKeyFlags = C.MOD_NONE
  369. f = checkKeyFlags(value[i])
  370. flags = (C.MMKeyFlags)(flags | f)
  371. }
  372. return
  373. }
  374. func keyTaps(k string, keyArr []string, pid int) error {
  375. flags := getFlagsFromValue(keyArr)
  376. key, err := checkKeyCodes(k)
  377. if err != nil {
  378. return err
  379. }
  380. tapKeyCode(key, flags, C.uintptr(pid))
  381. MilliSleep(KeySleep)
  382. return nil
  383. }
  384. func keyToggles(k string, keyArr []string, pid int, args ...interface{}) error {
  385. if len(keyArr) <= 0 {
  386. keyArr = append(keyArr, "down")
  387. }
  388. down := true
  389. if keyArr[0] == "up" {
  390. down = false
  391. }
  392. if keyArr[0] == "up" || keyArr[0] == "down" {
  393. keyArr = keyArr[1:]
  394. }
  395. flags := getFlagsFromValue(keyArr)
  396. key, err := checkKeyCodes(k)
  397. if err != nil {
  398. return err
  399. }
  400. C.toggleKeyCode(key, C.bool(down), flags, C.uintptr(pid))
  401. if len(args) > 0 {
  402. MilliSleep(KeySleep)
  403. }
  404. return nil
  405. }
  406. /*
  407. __ ___ ___________ ____ .______ ______ ___ .______ _______
  408. | |/ / | ____\ \ / / | _ \ / __ \ / \ | _ \ | \
  409. | ' / | |__ \ \/ / | |_) | | | | | / ^ \ | |_) | | .--. |
  410. | < | __| \_ _/ | _ < | | | | / /_\ \ | / | | | |
  411. | . \ | |____ | | | |_) | | `--' | / _____ \ | |\ \----.| '--' |
  412. |__|\__\ |_______| |__| |______/ \______/ /__/ \__\ | _| `._____||_______/
  413. */
  414. // ToInterfaces convert []string to []interface{}
  415. func ToInterfaces(fields []string) []interface{} {
  416. res := make([]interface{}, 0, len(fields))
  417. for _, s := range fields {
  418. res = append(res, s)
  419. }
  420. return res
  421. }
  422. // ToStrings convert []interface{} to []string
  423. func ToStrings(fields []interface{}) []string {
  424. res := make([]string, 0, len(fields))
  425. for _, s := range fields {
  426. res = append(res, s.(string))
  427. }
  428. return res
  429. }
  430. // toErr it converts a C string to a Go error
  431. func toErr(str *C.char) error {
  432. gstr := C.GoString(str)
  433. if gstr == "" {
  434. return nil
  435. }
  436. return errors.New(gstr)
  437. }
  438. // KeyTap taps the keyboard code;
  439. //
  440. // See keys supported:
  441. //
  442. // https://github.com/go-vgo/robotgo/blob/master/docs/keys.md#keys
  443. //
  444. // Examples:
  445. //
  446. // robotgo.KeySleep = 100 // 100 millisecond
  447. // robotgo.KeyTap("a")
  448. // robotgo.KeyTap("i", "alt", "command")
  449. //
  450. // arr := []string{"alt", "command"}
  451. // robotgo.KeyTap("i", arr)
  452. //
  453. // robotgo.KeyTap("k", pid int)
  454. func KeyTap(key string, args ...interface{}) error {
  455. var keyArr []string
  456. if len(key) > 0 && unicode.IsUpper([]rune(key)[0]) {
  457. args = append(args, "shift")
  458. }
  459. key = strings.ToLower(key)
  460. if _, ok := Special[key]; ok {
  461. key = Special[key]
  462. if len(args) <= 0 {
  463. args = append(args, "shift")
  464. }
  465. }
  466. pid := 0
  467. if len(args) > 0 {
  468. if reflect.TypeOf(args[0]) == reflect.TypeOf(keyArr) {
  469. keyArr = args[0].([]string)
  470. } else {
  471. if reflect.TypeOf(args[0]) == reflect.TypeOf(pid) {
  472. pid = args[0].(int)
  473. keyArr = ToStrings(args[1:])
  474. } else {
  475. keyArr = ToStrings(args)
  476. }
  477. }
  478. }
  479. return keyTaps(key, keyArr, pid)
  480. }
  481. // KeyToggle toggles the keyboard, if there not have args default is "down"
  482. //
  483. // See keys:
  484. //
  485. // https://github.com/go-vgo/robotgo/blob/master/docs/keys.md#keys
  486. //
  487. // Examples:
  488. //
  489. // robotgo.KeyToggle("a")
  490. // robotgo.KeyToggle("a", "up")
  491. //
  492. // robotgo.KeyToggle("a", "up", "alt", "cmd")
  493. // robotgo.KeyToggle("k", pid int)
  494. func KeyToggle(key string, args ...interface{}) error {
  495. if len(key) > 0 && unicode.IsUpper([]rune(key)[0]) {
  496. args = append(args, "shift")
  497. }
  498. key = strings.ToLower(key)
  499. if _, ok := Special[key]; ok {
  500. key = Special[key]
  501. if len(args) <= 1 {
  502. args = append(args, "shift")
  503. }
  504. }
  505. pid := 0
  506. var keyArr []string
  507. if len(args) > 0 && reflect.TypeOf(args[0]) == reflect.TypeOf(pid) {
  508. pid = args[0].(int)
  509. keyArr = ToStrings(args[1:])
  510. } else {
  511. keyArr = ToStrings(args)
  512. }
  513. return keyToggles(key, keyArr, pid)
  514. }
  515. // KeyPress press key string
  516. func KeyPress(key string, args ...interface{}) error {
  517. err := KeyDown(key, args...)
  518. if err != nil {
  519. return err
  520. }
  521. MilliSleep(1 + rand.Intn(3))
  522. return KeyUp(key, args...)
  523. }
  524. // KeyDown press down a key
  525. func KeyDown(key string, args ...interface{}) error {
  526. return KeyToggle(key, args...)
  527. }
  528. // KeyUp press up a key
  529. func KeyUp(key string, args ...interface{}) error {
  530. arr := []interface{}{"up"}
  531. arr = append(arr, args...)
  532. return KeyToggle(key, arr...)
  533. }
  534. // ReadAll read string from clipboard
  535. func ReadAll() (string, error) {
  536. return clipboard.ReadAll()
  537. }
  538. // WriteAll write string to clipboard
  539. func WriteAll(text string) error {
  540. return clipboard.WriteAll(text)
  541. }
  542. // CharCodeAt char code at utf-8
  543. func CharCodeAt(s string, n int) rune {
  544. i := 0
  545. for _, r := range s {
  546. if i == n {
  547. return r
  548. }
  549. i++
  550. }
  551. return 0
  552. }
  553. // UnicodeType tap the uint32 unicode
  554. func UnicodeType(str uint32, args ...int) {
  555. cstr := C.uint(str)
  556. pid := 0
  557. if len(args) > 0 {
  558. pid = args[0]
  559. }
  560. isPid := 0
  561. if len(args) > 1 {
  562. isPid = args[1]
  563. }
  564. C.unicodeType(cstr, C.uintptr(pid), C.int8_t(isPid))
  565. }
  566. // ToUC trans string to unicode []string
  567. func ToUC(text string) []string {
  568. var uc []string
  569. for _, r := range text {
  570. textQ := strconv.QuoteToASCII(string(r))
  571. textUnQ := textQ[1 : len(textQ)-1]
  572. st := strings.Replace(textUnQ, "\\u", "U", -1)
  573. if st == "\\\\" {
  574. st = "\\"
  575. }
  576. if st == `\"` {
  577. st = `"`
  578. }
  579. uc = append(uc, st)
  580. }
  581. return uc
  582. }
  583. func inputUTF(str string) {
  584. cstr := C.CString(str)
  585. C.input_utf(cstr)
  586. C.free(unsafe.Pointer(cstr))
  587. }
  588. // TypeStr send a string (supported UTF-8)
  589. //
  590. // robotgo.TypeStr(string: "The string to send", int: pid, "milli_sleep time", "x11 option")
  591. //
  592. // Examples:
  593. //
  594. // robotgo.TypeStr("abc@123, Hi galaxy, こんにちは")
  595. // robotgo.TypeStr("To be or not to be, this is questions.", pid int)
  596. func TypeStr(str string, args ...int) {
  597. var tm, tm1 = 0, 7
  598. if len(args) > 1 {
  599. tm = args[1]
  600. }
  601. if len(args) > 2 {
  602. tm1 = args[2]
  603. }
  604. pid := 0
  605. if len(args) > 0 {
  606. pid = args[0]
  607. }
  608. if runtime.GOOS == "linux" {
  609. strUc := ToUC(str)
  610. for i := 0; i < len(strUc); i++ {
  611. ru := []rune(strUc[i])
  612. if len(ru) <= 1 {
  613. ustr := uint32(CharCodeAt(strUc[i], 0))
  614. UnicodeType(ustr, pid)
  615. } else {
  616. inputUTF(strUc[i])
  617. MilliSleep(tm1)
  618. }
  619. MilliSleep(tm)
  620. }
  621. return
  622. }
  623. for i := 0; i < len([]rune(str)); i++ {
  624. ustr := uint32(CharCodeAt(str, i))
  625. UnicodeType(ustr, pid)
  626. // if len(args) > 0 {
  627. MilliSleep(tm)
  628. // }
  629. }
  630. MilliSleep(KeySleep)
  631. }
  632. // PasteStr paste a string (support UTF-8),
  633. // write the string to clipboard and tap `cmd + v`
  634. func PasteStr(str string) error {
  635. err := clipboard.WriteAll(str)
  636. if err != nil {
  637. return err
  638. }
  639. if runtime.GOOS == "darwin" {
  640. return KeyTap("v", "command")
  641. }
  642. return KeyTap("v", "control")
  643. }
  644. // TypeStrDelay type string with delayed
  645. // And you can use robotgo.KeySleep = 100 to delayed not this function
  646. func TypeStrDelay(str string, delay int) {
  647. TypeStr(str)
  648. MilliSleep(delay)
  649. }
  650. // SetDelay sets the key and mouse delay
  651. // robotgo.SetDelay(100) option the robotgo.KeySleep and robotgo.MouseSleep = d
  652. func SetDelay(d ...int) {
  653. v := 10
  654. if len(d) > 0 {
  655. v = d[0]
  656. }
  657. KeySleep = v
  658. MouseSleep = v
  659. }