Skip to content

Instantly share code, notes, and snippets.

@albb0920
Created February 28, 2011 11:57
Show Gist options
  • Save albb0920/847223 to your computer and use it in GitHub Desktop.
Save albb0920/847223 to your computer and use it in GitHub Desktop.
MetaKeyKeyListener example
package tw.loli.inputTest;
import android.content.Context;
import android.graphics.Color;
import android.text.Editable;
import android.text.InputType;
import android.text.method.MetaKeyKeyListener;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.BaseInputConnection;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;
class TestView extends View{
private String TAG = "TEST";
private long metaState = 0;
public TestView(Context context) {
super(context);
setBackgroundColor(Color.GREEN);
setFocusable(true);
setFocusableInTouchMode(true);
}
public boolean onKeyDown (int keyCode, KeyEvent event){
long newState = MetaKeyKeyListener.handleKeyDown(metaState, keyCode, event);
if(metaState == newState){ // Normal Key press
int c = event.getUnicodeChar(MetaKeyKeyListener.getMetaState(newState));
int[] test = new int[1];test[0] = c;
Log.v(TAG,"Got: "+new String(test,0,1));
metaState = MetaKeyKeyListener.adjustMetaAfterKeypress(metaState);
}else
metaState = newState;
return true;
}
public boolean onKeyUp (int keyCode, KeyEvent event){
metaState = MetaKeyKeyListener.handleKeyUp(metaState, keyCode, event);
Log.v(TAG,"Key up meta : "+MetaKeyKeyListener.getMetaState(metaState));
return super.onKeyUp(keyCode, event);
}
public boolean onCheckIsTextEditor (){
return true;
}
public InputConnection onCreateInputConnection (EditorInfo outAttrs){
outAttrs.imeOptions = EditorInfo.IME_FLAG_NO_EXTRACT_UI;
outAttrs.inputType = InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS;
return new TestInputConnection(false);
}
class TestInputConnection extends BaseInputConnection{
private Editable dummyEditable;
public TestInputConnection(boolean fullEditor) {
super(TestView.this, fullEditor);
Log.v(TAG,"new connection start.");
}
}
}
@prithivraj
Copy link

Predictive text is not working with this code. Help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment