Skip to content Skip to sidebar Skip to footer

Xamarin Custom Keyboard

I am trying to create a custom keyboard for a specific page and I have some problems in handling the listener for all the keys and the documentations for this is very limited. I am

Solution 1:

As the name of the listener indicates, it is an Interface you need to implement. C# does currently not allow for anonymous classes just like Java does.

Remember when implementing Java Interfaces you need to inherit from Java.Lang.Object, as it expects a handle.

So your implementation could look something like:

publicclassKeyboardOnKeyEventArgs : KeyboardKeyCodeEventArgs
{
    public Keycode[] KeyCodes { get; set; }
}

publicclassKeyboardKeyCodeEventArgs : EventArgs
{
    public Keycode PrimaryCode { get; set; }
}

publicclassKeyboardOnTextEventArgs : EventArgs
{
    public ICharSequence Text { get; set; }
}

publicclassMyKeyboardListener : Java.Lang.Object, KeyboardView.IOnKeyboardActionListener
{
    publicevent EventHandler<KeyboardOnKeyEventArgs> Key;
    publicevent EventHandler<KeyboardKeyCodeEventArgs> Press;
    publicevent EventHandler<KeyboardKeyCodeEventArgs> Release;
    publicevent EventHandler<KeyboardOnTextEventArgs> Text;
    publicevent EventHandler OnSwipeDown;
    publicevent EventHandler OnSwipeLeft;
    publicevent EventHandler OnSwipeRight;
    publicevent EventHandler OnSwipeUp;

    publicvoidOnKey(Keycode primaryCode, Keycode[] keyCodes)
    {
        if (Key != null)
            Key(this, new KeyboardOnKeyEventArgs {
                KeyCodes = keyCodes,
                PrimaryCode = primaryCode
            });
    }

    publicvoidOnPress(Keycode primaryCode)
    {
        if (Press != null)
            Press(this, new KeyboardKeyCodeEventArgs { PrimaryCode = primaryCode });
    }

    publicvoidOnRelease(Keycode primaryCode)
    {
        if (Release != null)
            Release(this, new KeyboardKeyCodeEventArgs { PrimaryCode = primaryCode });
    }

    publicvoidOnText(ICharSequence text)
    {
        if (Text != null)
            Text(this, new KeyboardOnTextEventArgs {Text = text});

    }

    publicvoidSwipeDown()
    {
        if(OnSwipeDown != null)
            OnSwipeDown(this, EventArgs.Empty);
    }

    publicvoidSwipeLeft()
    {
        if (OnSwipeLeft != null)
            OnSwipeLeft(this, EventArgs.Empty);
    }

    publicvoidSwipeRight()
    {
        if (OnSwipeRight != null)
            OnSwipeRight(this, EventArgs.Empty);
    }

    publicvoidSwipeUp()
    {
        if (OnSwipeUp != null)
            OnSwipeUp(this, EventArgs.Empty);
    }
}

Usage would be:

var keyboardListener = newMyKeyboardListener();
keyboardListener.Press += (s, e) => { /* do something */ };
mKeyboardView.OnKeyboardActionListener = keyboardListener;

EDIT:

Worked a bit more with it, and I cannot reproduce your error.

keyboard.xml

<?xml version="1.0" encoding="utf-8" ?><Keyboardxmlns:android="http://schemas.android.com/apk/res/android"android:keyWidth="33%p"android:horizontalGap="0px"android:verticalGap="0px"android:keyHeight="54dip"><Row><Keyandroid:codes="8"android:keyLabel="1"android:keyEdgeFlags="left" /><Keyandroid:codes="9"android:keyLabel="2" /><Keyandroid:codes="10"android:keyLabel="3"android:keyEdgeFlags="right" /></Row><Row><Keyandroid:codes="11"android:keyLabel="4"android:keyEdgeFlags="left" /><Keyandroid:codes="12"android:keyLabel="5" /><Keyandroid:codes="13"android:keyLabel="6"android:keyEdgeFlags="right" /></Row><Row><Keyandroid:codes="14"android:keyLabel="7"android:keyEdgeFlags="left" /><Keyandroid:codes="15"android:keyLabel="8" /><Keyandroid:codes="16"android:keyLabel="9"android:keyEdgeFlags="right" /></Row></Keyboard>

Main.axml

<?xml version="1.0" encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><EditTextandroid:layout_above="@+id/keyboard_view"android:layout_alignParentTop="true"android:layout_width="match_parent"android:id="@+id/target"android:layout_height="wrap_content" /><android.inputmethodservice.KeyboardViewandroid:id="@+id/keyboard_view"android:visibility="gone"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_alignParentBottom="true" /></RelativeLayout>

slide_up.xml

<?xml version="1.0" encoding="utf-8" ?><setxmlns:android="http://schemas.android.com/apk/res/android"><translateandroid:fromYDelta="-50%p"android:toYDelta="0"android:duration="200"/><alphaandroid:fromAlpha="0.0"android:toAlpha="1.0"android:duration="200" /></set>

MyKeyboardListener.cs

publicclassMyKeyboardListener : Java.Lang.Object, KeyboardView.IOnKeyboardActionListener
{
    privatereadonly Activity _activity;

    publicMyKeyboardListener(Activity activity) {
        _activity = activity;
    }

    publicvoidOnKey(Keycode primaryCode, Keycode[] keyCodes)
    {
        var eventTime = DateTime.Now.Ticks;
        var keyEvent = new KeyEvent(eventTime, eventTime, KeyEventActions.Down, primaryCode, 0);
        _activity.DispatchKeyEvent(keyEvent);
    }

    publicvoidOnPress(Keycode primaryCode) { }
    publicvoidOnRelease(Keycode primaryCode) { }
    publicvoidOnText(ICharSequence text) { }
    publicvoidSwipeDown() { }
    publicvoidSwipeLeft() { }
    publicvoidSwipeRight() { }
    publicvoidSwipeUp() { }
}

MainActivity.cs

[Activity(Label = "App14", MainLauncher = true, Icon = "@drawable/icon")]
publicclassMainActivity : Activity
{
    private Keyboard _keyBoard;
    private EditText _targetView;
    private KeyboardView _keyboardView;

    protectedoverridevoidOnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.Main);

        _keyBoard = new Keyboard(this, Resource.Xml.keyboard);
        _keyboardView = FindViewById<KeyboardView>(Resource.Id.keyboard_view);
        _keyboardView.Keyboard = _keyBoard;
        _keyboardView.OnKeyboardActionListener = new MyKeyboardListener(this);
        _targetView = FindViewById<EditText>(Resource.Id.target);
        _targetView.Touch += (sender, args) => {
            var bottomUp = AnimationUtils.LoadAnimation(this, Resource.Animation.slide_up);
            _keyboardView.StartAnimation(bottomUp);
            _keyboardView.Visibility = ViewStates.Visible;
            args.Handled = true;
        };
    }
}

Keyboard shows and characters are dispatched to the EditText.

Solution 2:

On how to hide the soft keyboard from popping up Add this to your Activity attributes:

WindowSoftInputMode = Android.Views.SoftInput.StateAlwaysHidden

Like this:

Activity(MainLauncher = true, WindowSoftInputModeAndroid.Views.SoftInput.StateAlwaysHidden)

Post a Comment for "Xamarin Custom Keyboard"