add linux typestr utf support

This commit is contained in:
vcaesar 2018-04-20 22:38:40 +08:00
parent 363c538275
commit fda0ba08a3
2 changed files with 61 additions and 3 deletions

View File

@ -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

View File

@ -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)
}
}
}