Java JNI - Calling object method
I'm currently programming an interface between some C++ code and Java using JNI.
I'm getting some events in my GUI that I want to pass to a C++ event handler.
I therefore call a function that I wrote in Java.
public void sendToEventQueue( AWTEvent evt ) {
Mudkiptz.Main.fctC_sendEvent( evt );
}
This method is in an abstract class EventHdl that I used as a super class for keyEventHandler in which I overload the keyTyped( KeyEvent) to send the KeyEvent to my C++ event handler with the method previously declared.
My problem is that I want to get my keyEvent keyCode from the KeyEvent that I passed.
JNIEXPORT void JNICALL Java_Mudkiptz_Main_fctC_1sendEvent
(JNIEnv* env, jclass, jobject evt) {
// Obtenir les infos (Get information)
jclass keyEventClass = 开发者_开发百科env->FindClass("java/awt/event/KeyEvent");
if( env->IsInstanceOf(evt, keyEventClass) ) {
jmethodID getKeyCode = env->GetMethodID(keyEventClass, "getKeyCode", "()I");
int keyCode = 0;
keyCode = env->CallIntMethod(evt, getKeyCode);
}
// getInstance()
Application::obtenirInstance()->getEventQueue()->push( evt );
}
But it doesn't not work... :( I'm not used to JNI so it may be a easy mistake nevertheless I would really appreciate help.
Edit: I should have been more precise. The method return but keyCode always equals zero when it should give the keyCode. thanks!
Thanks!
I finally found what I was doing wrong. It seems that getKeyCode wasn't the function I was searching for.
To debug, I went to the java call and print the event to string and keycode always equals 0 even in java. So, I check the dump and keyChar had the correct value of my input. So I changed the call to use getKeyChar instead and everything is working fine.
Thanks for your help!
精彩评论