Android : Set focus on Custom Soft Keyboard's key when it pops up

on Monday, January 19, 2015


I am working on an idea where I need to popup a soft keyboard when edit text is clicked which works fine. My next target is to get any key selected of the soft keyboard when it pops up like key 'q' should have focus or should be highlighted by default when keyboard pops up. It is a custom Soft Keyboard. I have tried putting tag in the key's source however it doesn't recognize it.My target is to enable navigation on this custom keyboard through a D-pad which is sending key-codes over blue tooth.To enable the navigation I need to get focus on soft keyboard I want to know how can achieve Below is my XML and Java code.Any ideas in this direction would be really appreciated:



<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
android:keyWidth="10%p"
android:horizontalGap="0px"
android:verticalGap="0px"
android:keyHeight="60dp"

>
<Row>
<Key android:codes="49" android:keyLabel="1" android:keyEdgeFlags="left"/>
<Key android:codes="50" android:keyLabel="2"/>
<Key android:codes="51" android:keyLabel="3"/>
<Key android:codes="52" android:keyLabel="4"/>
<Key android:codes="53" android:keyLabel="5"/>
<Key android:codes="54" android:keyLabel="6"/>
<Key android:codes="55" android:keyLabel="7"/>
<Key android:codes="56" android:keyLabel="8"/>
<Key android:codes="57" android:keyLabel="9"/>
<Key android:codes="48" android:keyLabel="0" android:keyEdgeFlags="right"/>
</Row>
<Row>
<Key android:codes="113" android:keyLabel="q" android:keyEdgeFlags="left"

/>
<Key android:codes="119" android:keyLabel="w"/>
<Key android:codes="101" android:keyLabel="e"/>
<Key android:codes="114" android:keyLabel="r"/>
<Key android:codes="116" android:keyLabel="t"/>
<Key android:codes="121" android:keyLabel="y"/>
<Key android:codes="117" android:keyLabel="u"/>
<Key android:codes="105" android:keyLabel="i"/>
<Key android:codes="111" android:keyLabel="o"/>
<Key android:codes="112" android:keyLabel="p" android:keyEdgeFlags="right"/>
</Row>
<Row>
<Key android:codes="97" android:keyLabel="a" android:keyEdgeFlags="left"/>
<Key android:codes="115" android:keyLabel="s"/>
<Key android:codes="100" android:keyLabel="d"/>
<Key android:codes="102" android:keyLabel="f"/>
<Key android:codes="103" android:keyLabel="g"/>
<Key android:codes="104" android:keyLabel="h"/>
<Key android:codes="106" android:keyLabel="j"/>
<Key android:codes="107" android:keyLabel="k"/>
<Key android:codes="108" android:keyLabel="l"/>
<Key android:codes="35,64" android:keyLabel="\# \@" android:keyEdgeFlags="right"/>
</Row>
<Row>
<Key android:codes="-1" android:keyLabel="CAPS" android:keyEdgeFlags="left"/>
<Key android:codes="122" android:keyLabel="z"/>
<Key android:codes="120" android:keyLabel="x"/>
<Key android:codes="99" android:keyLabel="c"/>
<Key android:codes="118" android:keyLabel="v"/>
<Key android:codes="98" android:keyLabel="b"/>
<Key android:codes="110" android:keyLabel="n"/>
<Key android:codes="109" android:keyLabel="m"/>
<Key android:codes="46" android:keyLabel="."/>
<Key android:codes="63,33,58" android:keyLabel="\? ! :" android:keyEdgeFlags="right"/>
</Row>
<Row android:rowEdgeFlags="bottom">
<Key android:codes="44" android:keyLabel="," android:keyWidth="10%p" android:keyEdgeFlags="left"/>
<Key android:codes="47" android:keyLabel="/" android:keyWidth="10%p" />
<Key android:codes="32" android:keyLabel="SPACE" android:keyWidth="40%p" android:isRepeatable="true"/>
<Key android:codes="-5" android:keyLabel="DEL" android:keyWidth="20%p" android:isRepeatable="true"/>
<Key android:codes="-4" android:keyLabel="DONE" android:keyWidth="20%p" android:keyEdgeFlags="right"/>
</Row>

</Keyboard>


This is the java copackage com.example.keyboard;



package com.example.keyboard;

import android.inputmethodservice.InputMethodService;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.inputmethodservice.KeyboardView.OnKeyboardActionListener;
import android.media.AudioManager;
import android.view.View;
import android.view.inputmethod.InputConnection;

public class SimpleIME extends InputMethodService implements
OnKeyboardActionListener {
private KeyboardView kv;
private Keyboard keyboard;

@Override
public View onCreateInputView() {
kv = (KeyboardView) getLayoutInflater()
.inflate(R.layout.keyboard, null);
keyboard = new Keyboard(this, R.xml.qwerty);
kv.setKeyboard(keyboard);
kv.setOnKeyboardActionListener(this);
/*
* kv.setClickable(true); kv.setFocusable(true);
* kv.setFocusableInTouchMode(true); kv.setEnabled(true);
*/

kv.focusSearch(113);
return kv;
}

@Override
public void onKey(int primaryCode, int[] keyCodes) {
// TODO Auto-generated method stub
InputConnection ic = getCurrentInputConnection();
playClick(primaryCode);
switch (primaryCode) {
case Keyboard.KEYCODE_DELETE:
ic.deleteSurroundingText(1, 0);
break;
case Keyboard.KEYCODE_SHIFT:
/*
* caps = !caps; keyboard.setShifted(caps); kv.invalidateAllKeys();
* break;
*/
case Keyboard.KEYCODE_DONE:
/*
* ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN,
* KeyEvent.KEYCODE_ENTER)); break;
*/
default:
char code = (char) primaryCode;
/*
* if(Character.isLetter(code) && caps){ code =
* Character.toUpperCase(code); }
*/
ic.commitText(String.valueOf(code), 1);
}
}

@Override
public void onPress(int primaryCode) {
// TODO Auto-generated method stub

}

@Override
public void onRelease(int primaryCode) {
// TODO Auto-generated method stub

}

@Override
public void onText(CharSequence text) {
// TODO Auto-generated method stub

}

@Override
public void swipeDown() {
// TODO Auto-generated method stub

}

@Override
public void swipeLeft() {
// TODO Auto-generated method stub

}

@Override
public void swipeRight() {
// TODO Auto-generated method stub

}

@Override
public void swipeUp() {
// TODO Auto-generated method stub

}

private void playClick(int keyCode) {
AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
switch (keyCode) {
case 32:
am.playSoundEffect(AudioManager.FX_KEYPRESS_SPACEBAR);
break;
case Keyboard.KEYCODE_DONE:
case 10:
am.playSoundEffect(AudioManager.FX_KEYPRESS_RETURN);
break;
case Keyboard.KEYCODE_DELETE:
am.playSoundEffect(AudioManager.FX_KEYPRESS_DELETE);
break;
default:
am.playSoundEffect(AudioManager.FX_KEYPRESS_STANDARD);
}
}

}


Thanks in advance


0 comments:

Post a Comment