From fda0ba08a3617ad52b99ee2b30ef6afcb180f48b Mon Sep 17 00:00:00 2001 From: vcaesar Date: Fri, 20 Apr 2018 22:38:40 +0800 Subject: [PATCH] add linux typestr utf support --- key/keypress_c.h | 33 +++++++++++++++++++++++++++++++++ robotgo.go | 31 ++++++++++++++++++++++++++++--- 2 files changed, 61 insertions(+), 3 deletions(-) diff --git a/key/keypress_c.h b/key/keypress_c.h index ae0fdaa..f41673e 100644 --- a/key/keypress_c.h +++ b/key/keypress_c.h @@ -214,6 +214,39 @@ void toggleUnicode(UniChar ch, const bool down) #if defined(USE_X11) #define toggleUniKey(c, down) toggleKey(c, down, MOD_NONE) + + int input_utf(const char *utf) { + Display *dpy; + dpy = XOpenDisplay(NULL); + + KeySym sym = XStringToKeysym(utf); + // KeySym sym = XKeycodeToKeysym(dpy, utf); + + int min, max, numcodes; + XDisplayKeycodes(dpy, &min, &max); + KeySym *keysym; + keysym = XGetKeyboardMapping(dpy, min,max-min+1, &numcodes); + keysym[(max-min-1)*numcodes]=sym; + XChangeKeyboardMapping(dpy, min, numcodes, keysym, (max-min)); + XFree(keysym); + XFlush(dpy); + + KeyCode code = XKeysymToKeycode(dpy, sym); + + XTestFakeKeyEvent(dpy, code, True, 1); + XTestFakeKeyEvent(dpy, code, False, 1); + + XFlush(dpy); + + XCloseDisplay(dpy); + + return 0; + } +#endif +#if !defined(USE_X11) + int input_utf(const char *utf){ + return 0; + } #endif // unicode type diff --git a/robotgo.go b/robotgo.go index a6b2c1f..ee645ee 100644 --- a/robotgo.go +++ b/robotgo.go @@ -54,6 +54,7 @@ import ( "os" "reflect" "runtime" + "strconv" "strings" "time" "unsafe" @@ -691,11 +692,35 @@ func CharCodeAt(s string, n int) rune { return 0 } +func toUc(text string) []string { + var uc []string + + textQuoted := strconv.QuoteToASCII(text) + textUnquoted := textQuoted[1 : len(textQuoted)-1] + + sUnicodev := strings.Split(textUnquoted, "\\u") + for i := 1; i < len(sUnicodev); i++ { + uc = append(uc, "U"+sUnicodev[i]) + } + + return uc +} + // TypeStr type string, support UTF-8 func TypeStr(str string) { - for i := 0; i < len([]rune(str)); i++ { - ustr := uint32(CharCodeAt(str, i)) - UnicodeType(ustr) + if runtime.GOOS == "linux" { + strUc := toUc(str) + for i := 0; i < len(strUc); i++ { + cstr := C.CString(strUc[i]) + C.input_utf(cstr) + + defer C.free(unsafe.Pointer(cstr)) + } + } else { + for i := 0; i < len([]rune(str)); i++ { + ustr := uint32(CharCodeAt(str, i)) + UnicodeType(ustr) + } } }