Estou escrevendo um aplicativo que envia pressionamentos de tecla do teclado virtual do Android para algum cliente. Atualmente estou implementando o cliente Ubuntu, enviando eventos% input x11
falsos. Eu tenho um problema ao converter caracteres UTF-8 especiais ou qualquer alfabeto não padrão (teste usando um cirílico) para a representação interna x11.
A função XStringToKeysym
não funciona de todo. Tudo o que faz corretamente é converter caracteres latinos.
Eu tentei diferentes codificações, mas nenhuma delas funcionou completamente.
A mais recente abordagem é esta:
char *nativeString = ...; //utf-8 encoding
int k = 0;
unsigned long int code = 0;
char c;
while((c = nativeString[k++]) != 'String str = ...; // from socket
System.out.println(str); //prints out good
byte[] bytes = str.getBytes("UTF-8");
NativeCall.press(bytes,...);
' ){
code = code | ((c & 0xFF) << (k-1)*8);
}
Isso adiciona alguns caracteres de pontuação à lista de trabalho. Mesma coisa com o alfabeto cirílico.
Algum conselho?
EDITAR:
No lado java, recebo uma string do socket:
JNIEXPORT void JNICALL Java_NativeCall_press(JNIEnv* env, jobject obj, jbyteArray array, jint modifiers) {
Window winFocus;
int revert;
XGetInputFocus(display, &winFocus, &revert);
jboolean copy;
jbyte* bufferPtr = env->GetByteArrayElements(array, NULL);//got the bytes
jsize lengthOfArray = env->GetArrayLength(array);//length
for(int i = 0;i<lengthOfArray;i++){ //print the byte to make sure they are the same as in java code
cout << (bufferPtr[i] & 0xff) << endl;
}
KeySym code = XStringToKeysym((const char *) bufferPtr);//doesn't work
cout << "Code: " << code << endl; //way #1
/*unsigned long int code = 0; //way #2, works with some symbols
for(int i = 0;i<lengthOfArray;i++){
char c = bufferPtr[i];
code = code | ((c & 0xFF) << i*8);
}*/
env->ReleaseByteArrayElements(array, bufferPtr, JNI_ABORT);
//send event, checked, works fine
XKeyEvent event = createKeyEvent(display, winFocus, root_window, true, code, modifiers);
XSendEvent(event.display, event.window, True, KeyPressMask, (XEvent *)&event);
XFlush(display);
event = createKeyEvent(display, winFocus, root_window, false, code, modifiers);
XSendEvent(event.display, event.window, True, KeyPressMask, (XEvent *)&event);
XFlush(display);
}
No lado nativo:
%pre%