The service code that inherits from the Inputmethodservice class is as follows:
int keycode = Skey.getkeycode ();
KeyEvent Edown = new KeyEvent (0, 0, Keyevent.action_down, keycode, 0, 0, 0, 0, keyevent.flag_soft_keyboard); KeyEvent eUp = new KeyEvent (0, 0, keyevent.action_up, KeyCode, 0, 0, 0, 0, keyevent.flag_soft_keyboard); OnKeyDown (KeyCode, edown); OnKeyUp (KeyCode, eUp);
The above code: send the relevant key to the application, that is, the application of listening Input method key events
@Override Public boolean onKeyDown (int keycode, keyevent event) { if (Processkey (event, 0! = Event.getrepeatcount ())) return true; Return Super.onkeydown (KeyCode, event); } @Override Public boolean onKeyUp (int keycode, keyevent event) { if (Processkey (event, True)) return true; Return Super.onkeyup (KeyCode, event); }
Above the down and up, we focus on onkeyup, tracking down the log, is the following directly return True
if (Processkey (event, True)) return true;
Enter the method, where the following code is used to overwrite the code:
if (Processfunctionkeys (KeyCode, realaction)) { return true; }
Then go into the method Processfunctionkeys, follow it into the following code:
if (keycode = = keyevent.keycode_enter) { if (!realaction) { log.d (TAG, "Processfunctionkeys call Keycode_enter Return "); return true; } Sendkeychar (' \ n '); return true; }
Because the above realaction is passed in as true, the following is performed
Sendkeychar (' \ n ');
Into this method, the method in Inputmethodservice
public void Sendkeychar (char charcode) { switch (charcode) {case ' \ n '://Apps may be listening to an enter key t o Perform an action if (!senddefaulteditoraction (True)) { senddownupkeyevents (keyevent.keycode_enter); } break; Default: //Make sure this digits go through any text watcher on the client side. if (charcode >= ' 0 ' && charcode <= ' 9 ') { senddownupkeyevents (CharCode-' 0 ' + keyevent.keycode_0);
} else { inputconnection ic = getcurrentinputconnection (); if (IC! = null) { Ic.committext (string.valueof (char) charcode), 1); } } break; } }
Because the pass-through is ' \ n ', it is executed as follows:
Case ' \ n ':////Apps May is listening to a enter key to perform an action if (!senddefaulteditoraction (True)) { sen Ddownupkeyevents (Keyevent.keycode_enter); } Break
Look at the above note://Apps may be listening-an enter key-perform an action, very clear, oh, come to see this method
Public Boolean senddefaulteditoraction (Boolean Fromenterkey) { editorinfo ei = getcurrentinputeditorinfo (); if (ei! = null && (!fromenterkey | | (Ei.imeoptions & editorinfo.ime_flag_no_enter_action) = = 0) && (ei.imeoptions & editorinfo.ime_mask_action)! = Editorinfo.ime_action_none) { //If The ENTER key is pressed, and the editor has a default //ACTION Associated W ith pressing ENTER, then send it //explicit action instead of the key event. Inputconnection IC = getcurrentinputconnection (); if (IC! = null) { ic.performeditoraction (ei.imeoptions&editorinfo.ime_mask_action); } return true; } return false; }
The above method is just a notification: Let the editor execute it, indicating that it can do an action:
/** * The editor perform an action it had said it can do. * /public boolean performeditoraction (int editoraction);
/** * Set of bits in {@link #imeOptions}, provide alternative actions * associated with the "enter" key. This both helps the IME provide * Better feedback on what the ENTER key would do, and also allows it * to Provid e Alternative mechanisms for providing the command. */public static final int ime_mask_action = 0X000000FF;
Let's look at this method editableinputconnection, about why editableinputconnection look at my previous post will understand
@Override Public boolean performeditoraction (int actioncode) { if (DEBUG) log.v (TAG, "performeditoraction" + Actioncode); Mtextview.oneditoraction (Actioncode); return true; }
The general application wants to listen for a return or search button, as follows:
Edittext.setoneditoractionlistener (New Textview.oneditoractionlistener () {@Overridepublic Boolean oneditoraction ( TextView V, int ActionId, keyevent event) {/* Determines whether the "GO" key */if (ActionId = = Editorinfo.ime_action_go) {Edittext.settext (" Success "); return true;} return false;}});
The above call is the interface in TextView
/** * Interface definition for a callback to being invoked when an action was * performed on the editor. */Public interface Oneditoractionlistener { /** * called if an action is being performed. * * @param v The view that is clicked. * @param ActionId Identifier of the action. This would be either the * identifier you supplied, or {@link editorinfo#ime_null * Editorinfo.ime_null} if being Called due to the ENTER key * being pressed. * @param event If triggered by a enter key, this is the event; * Otherwise, this is null. * @return return True if you have consumed the action, else false. * /Boolean oneditoraction (TextView v, int ActionId, keyevent event); }
And how does it come true?
/** * Set a special listener to being called when an action was performed * on the text view. This would be called when the ENTER key was pressed, * or when a action supplied to the IME was selected by the User.
setting * This means then the normal hard key event won't insert a newline * into the text view even if it I s multi-line; Holding down the ALT * modifier would, however, allow the user to insert a newline character. */Public void Setoneditoractionlistener (Oneditoractionlistener l) { if (Minputcontenttype = = null) { Minputcontenttype = new Inputcontenttype (); } Minputcontenttype.oneditoractionlistener = l; }
We've seen Oneditoractionlistener L assigned to Minputcontenttype.oneditoractionlistener.
So let's go back to the method in Editableinputconnection above:
@Override Public boolean performeditoraction (int actioncode) { if (DEBUG) log.v (TAG, "performeditoraction" + Actioncode); Mtextview.oneditoraction (Actioncode); return true; }
Enter the following methods:
Mtextview.oneditoraction (Actioncode);
public void oneditoraction (int actioncode) { final inputcontenttype ICT = minputcontenttype; if (ICT! = NULL) { if (ict.oneditoractionlistener! = null) { if (ict.onEditorActionListener.onEditorAction (this , actioncode, null)) { return; } }
See, the Oneditoractionlistener, which was previously put into Minputcontenttype, is now assigned to Inputcontenttype ICT and then executed
Ict.onEditorActionListener.onEditorAction (This, actioncode, NULL)
Now we get it, search and enter, and so on, that's how callbacks are implemented.