Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting isSticky in Android custom keyboards

I have a custom keyboard with buttons that are isSticky enabled but I have problem detecting if it is turned on or off (true / false) and also disabling them after any key is pressed if it is turned on (true).

The problem with this is I can't find a way to detect the keys and also appending the current edittext with the functions (all the sticky buttons has specific functions).

This should happen in the OnKey function,

Here is my keyboard class:

public class MyKeyboardListener : Java.Lang.Object, KeyboardView.IOnKeyboardActionListener{

    private readonly Activity _activity;

    public MyKeyboardListener(Activity activity){
        _activity = activity;
    }

    public void OnKey(Android.Views.Keycode primaryCode, Android.Views.Keycode[] keyCodes){
        var eventTime = DateTime.Now.Ticks;
        var keyEvent = new KeyEvent(eventTime, eventTime, KeyEventActions.Down, primaryCode, 0);

        switch ((int)primaryCode) {
            case 1005:
            break;

            case 1006:
            break;

            default:
                _activity.DispatchKeyEvent(keyEvent);
            break;
        }
    }

    public void OnPress(Android.Views.Keycode primaryCode){
    }

    public void OnRelease(Android.Views.Keycode primaryCode){
    }

    public void OnText(Java.Lang.ICharSequence text){
    }

    public void SwipeDown(){
    }

    public void SwipeLeft(){
    }

    public void SwipeRight(){
    }

    public void SwipeUp(){
    }
}

Keyboard.axml

<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
android:keyWidth="100%"
android:keyHeight="6%p">
<Row>
    <Key
        android:codes="1000"
        android:keyLabel="A"
        android:keyEdgeFlags="left"
        android:isModifier="true" 
        android:isSticky="true"
        android:horizontalGap="1%p" />
    <Key
        android:codes="1001"
        android:keyLabel="B"
        android:isModifier="true" 
        android:isSticky="true"
        android:horizontalGap="1%p" />
    <Key
        android:codes="1002"
        android:keyLabel="C"
        android:isModifier="true" 
        android:isSticky="true"
        android:horizontalGap="1%p" />
    <Key
        android:codes="1003"
        android:keyLabel="D"
        android:isModifier="true" 
        android:isSticky="true"
        android:horizontalGap="1%p" />
    <Key
        android:codes="1004"
        android:keyLabel="E"
        android:keyEdgeFlags="right"
        android:isModifier="true" 
        android:isSticky="true"
        android:horizontalGap="1%p" />
</Row>
<Row>
    <Key
        android:codes="8"
        android:keyLabel="1"
        android:keyEdgeFlags="left"
        android:horizontalGap="1%p" />
    <Key
        android:codes="9"
        android:keyLabel="2"
        android:horizontalGap="1%p" />
    <Key
        android:codes="1005"
        android:keyLabel="F"
        android:horizontalGap="1%p" />
    <Key
        android:codes="1006"
        android:keyLabel="G46"
        android:horizontalGap="1%p" />
    <Key
        android:codes="67"
        android:keyLabel="DELETE"
        android:keyEdgeFlags="right"
        android:horizontalGap="1%p" />
</Row>
</Keyboard>
like image 485
Karl Wong Avatar asked Jan 21 '26 08:01

Karl Wong


1 Answers

Inside public class LatinKeyboardView extend KeyboardView, override onDraw method and use the following code to get the sticky and modifiers key

List<Key> keys = getKeyboard().getKeys();
for (Key key : keys) {
    Drawable npd;
    // int drawable = R.drawable.btn_normal_with_shadow_t1;
    int drawable = R.drawable.key_normal;
    textColor = sessionManager.getSimpleyKeyTextColor();

    if (key.pressed) {
        drawable = R.drawable.key_normal;
    } else {
        if (key.modifier && key.sticky) {
            drawable = R.drawable.stickey_with_shad_bord;
            textColor = sessionManager.getSimpleyKeyTextColorModifiers();
        } else if (key.modifier) {
            drawable = R.drawable.modifier_btn_with_bord_shad;
            textColor = sessionManager.getSimpleyKeyTextColorModifiers();
        } else {
            drawable = R.drawable.key_normal;
        }
    }

here is the xml code where we set modifier to true

<Key
    android:codes="-1"
    android:isModifier="true"
    android:keyWidth="14.8%p"
    android:keyEdgeFlags="left"
    android:keyIcon="@drawable/ic_shift_normal" />
like image 62
Badar Khalil Avatar answered Jan 23 '26 22:01

Badar Khalil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!