[SOLVED] Implement keyboard ofxAndroid

Hello to all,

sorry for the bad English, and my low experience.

I’m trying to implement within ofxAndroid the launch of the
keyboard.
Below is what I did, and all the files I touched:


This piece takes care of launching the system keyboard in android sdk.
ofxAndroid->ofAndroidLib->OFAndroid.java:

public void dyna_keyboardShow(){
		try{
		    final InputMethodManager inputMgr = (InputMethodManager)ofActivity.getSystemService(Context.INPUT_METHOD_SERVICE);
                    inputMgr.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
                    System.out.println("!!!!call soft keyboard!!!!");
		}catch(Exception e){
		    System.out.println("catch:"+e);
		}
	}

here the jni for communication with the method java
ofxAndroid->src->ofAppAndroidWindow.cpp:

void ofAppAndroidWindow::dyna_keyBoardShow() {
   ofLog(OF_LOG_NOTICE,"start keyboard show");
   jclass javaClass = ofGetJNIEnv()->FindClass("cc/openframeworks/OFAndroid");
   if(javaClass==0){
      ofLogError("ofAppAndroidWindow") << "dyna_keyboardShow(): couldn't find OFAndroid java class";
      return;
      }
   jmethodID method = ofGetJNIEnv()->GetMethodID(javaClass, "dyna_keyboardShow","()V");
   if(!method){
      ofLogError("ofAppAndroidWindow") << "dyna_keyboardShow(): couldn't find OFAndroid dyna_keyboardShowHide method";
      return;
      }
   else{
      ofGetJNIEnv()->NewObject(javaClass,method);
      }
   }

function declaration
ofAppAndroidWindow.h:

void dyna_keyBoardShow();

to make available the method level ofAppRunner I did this:
ofAppRunner.cpp:

void dynaKeyShow() {
   window->dyna_keyBoardShow();
}

function declaration ofAppRunner.h:

void dynaKeyShow();

Then I added the virtual in ofAppBaseWindow.h

virtual void dyna_keyBoardShow(){};

I noticed the keyPressed basis of OF succeeds in catching keyboard events
Android without the use of a “displathkeyevent” jni side, so I decided to unleash the keyboard on a ofxUITextInput, I have to do this trivially added to the inside of the method mousePressed between ifdef OFX_UI_TARGET_TOUCH of ofxUITextInput.cpp this:

dynaKeyShow();
ofLog(OF_LOG_NOTICE,"pressed UI");

I hope it is useful for someone :smile:
I will update the topic when I can to impute the TextInput.

good day to all.

Dario

1 Like

At the time I performed the following tests on the release of Android 4.1.2 - 4.0.3 - 2.3.6.

I’m still trying to understand why the event is not triggered keyPressed of ofxUITextInput, but I noticed that the event onKeyPressed father in ofxUIOFWrapper.cpp instead is interrogated properly.

and then you can use the keyPressed of openFrameworks for testing successfully.
good day
Dario

I was able to write in the TextInput ofxUI.

to launch the keyboard by tapping the TextInput
included in the mousePressed function of ofxUITextInput.cpp
between:

#ifdef OFX_UI_TARGET_TOUCH 

      / / .... 
     dynaKeyShow (); 

#endif 

to write to the TextInput
comment / if(bInsideCanvas) { / in the file ofxUICanvas.cpp function keyPressed

void ofxUICanvas::keyPressed(int key) { 
  /* if(bInsideCanvas) { */
      for(vector<ofxUIWidget *>::iterator it = widgets.begin(); it != widgets.end(); ++it) {
         (*it)->keyPressed(key);
         }
  /* } */
}

when you compile for Android.

bad solution, in short I try to write something with meaning,
and make a fork on git.

Ciao
Dario

:sunny: WONDERFULL :sunny:

I get it worked too.

Many thanks Dario

I’m glad you find it useful.
Unfortunately I have not had time to change ofxUI in a more clean.

Ciao Dario

@kashim Hi, have you used this on 0.8.3 and the latest ofxUI, I added all the lines as above but my app crashes when I call

dynaKeyShow ();

both from my ofApp and from the UI.

Fred

@fresla Hi,

I’m sorry this patch has been used only on 8.0.
anyway tell me the error and the release of Android in use, let’s take a look.

PS: I found only problems on Android 3.2 (crash application “galaxy tab”)

I wish you good day.
Dario

@kashim I was in the middle of developing and had to revert the changes to have everything running smooth. I will look into it again in a few days when I am a little further with the project.
Thanks a lot for the reply.

Fred

Just in case anyone looks i was not able to get this going on of 8.3 or 8.4 but instead went for straight java to solve the problem.

Sorry to necro the thread, but I modified the approach here for oF 0.9.3; here are my changes:

addons/ofxAndroid/ofAndroidLib/src/cc/openframeworks/OFAndroid.java

  private static Handler handler = new Handler();

  public static void dyna_keyboardToggle(){
    // https://stackoverflow.com/questions/39593324/cannot-resolve-symbol-showsoftinput
    handler.postDelayed(new Runnable() {
      @Override
      public void run() {
        try{
          System.out.println("!!!!call soft keyboard dyna_keyboardToggle!!!!");
          final InputMethodManager inputMgr = (InputMethodManager)ofActivity.getSystemService(Context.INPUT_METHOD_SERVICE);
          inputMgr.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
        }catch(Exception e){
          System.out.println("catch:"+e);
        }
      }
    },100);
  }

  public static void dyna_keyboardHide(){
    handler.postDelayed(new Runnable() {
      @Override
      public void run() {
        try{
          System.out.println("!!!!call soft keyboard dyna_keyboardHide!!!!");
          final InputMethodManager inputMgr = (InputMethodManager)ofActivity.getSystemService(Context.INPUT_METHOD_SERVICE);
          inputMgr.hideSoftInputFromWindow(ofActivity.getWindow().getDecorView().getRootView().getWindowToken(), 0);
        }catch(Exception e){
          System.out.println("catch:"+e);
        }
      }
    },100);
  }

addons/ofxAndroid/src/ofAppAndroidWindow.h:

  void dyna_keyboardToggle();
  void dyna_keyboardHide();

addons/ofxAndroid/src/ofAppAndroidWindow.cpp

void ofAppAndroidWindow::dyna_keyboardToggle() {
  ofLog(OF_LOG_NOTICE,"start keyboard show/toggle");
  jclass javaClass = ofGetJNIEnv()->FindClass("cc/openframeworks/OFAndroid");
  if(javaClass==0){
    ofLogError("ofAppAndroidWindow") << "dyna_keyboardToggle(): couldn't find OFAndroid java class";
    return;
  }
  //jmethodID method = ofGetJNIEnv()->GetMethodID(javaClass, "dyna_keyboardShow","()V");
  jmethodID method = ofGetJNIEnv()->GetStaticMethodID(javaClass, "dyna_keyboardToggle","()V");
  if(!method){
    ofLogError("ofAppAndroidWindow") << "dyna_keyboardToggle(): couldn't find OFAndroid dyna_keyboardShowHide method";
    return;
  }
  else{
    //ofGetJNIEnv()->NewObject(javaClass,method);
    ofGetJNIEnv()->CallStaticVoidMethod(javaClass,method);
  }
}

void ofAppAndroidWindow::dyna_keyboardHide() {
  ofLog(OF_LOG_NOTICE,"start keyboard hide");
  jclass javaClass = ofGetJNIEnv()->FindClass("cc/openframeworks/OFAndroid");
  if(javaClass==0){
    ofLogError("ofAppAndroidWindow") << "dyna_keyboardHide(): couldn't find OFAndroid java class";
    return;
  }
  jmethodID method = ofGetJNIEnv()->GetStaticMethodID(javaClass, "dyna_keyboardHide","()V");
  if(!method){
    ofLogError("ofAppAndroidWindow") << "dyna_keyboardHide(): couldn't find OFAndroid dyna_keyboardShowHide method";
    return;
  }
  else{
    ofGetJNIEnv()->CallStaticVoidMethod(javaClass,method);
  }
}

libs/openFrameworks/app/ofAppBaseWindow.h

    virtual void dyna_keyboardToggle(){};
    virtual void dyna_keyboardHide(){};

libs/openFrameworks/app/ofAppRunner.h

void ofAndroidSoftKeyboardToggle();
void ofAndroidSoftKeyboardHide();

libs/openFrameworks/app/ofAppRunner.cpp

void ofAndroidSoftKeyboardToggle() {
  #if defined(TARGET_ANDROID)
  mainLoop()->getCurrentWindow()->dyna_keyboardToggle();
  #endif
}

void ofAndroidSoftKeyboardHide() {
  #if defined(TARGET_ANDROID)
  mainLoop()->getCurrentWindow()->dyna_keyboardHide();
  #endif
}

Even if you use ofAndroidSoftKeyboardHide in a desktop build, they should compile, and simply do nothing in that case.