1

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!

3
  • Can you elaborate on "doesn't work"? What happens? What are you expecting to happen? Commented Jan 30, 2010 at 19:23
  • The method signature looks funny. I don't have the JNI signature rules in my head, but Java_Mudkiptz_Main_fctC_1sendEvent doesn't look right. Why is there a "1" in the middle of the function name? Commented Jan 30, 2010 at 19:25
  • Sorry, in fact, what hapens is that the method is calle but always return 0 when it should return the correct KeyCode. For the JNI signature, I don't understand the 1 in the declaration, but every other JNI method in my interface is working so I kind off pass over that. Thanks! Commented Jan 30, 2010 at 19:31

1 Answer 1

2

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!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.