Steffan Pallesen
Steffan Pallesen

Reputation: 550

Convert Keycode to char in Xamarin.Android

When a key is pressed in my custom View, OnKeyPress is called:

public DrawView(Context context, IAttributeSet attributeSet) : base(context, attributeSet)
{
        KeyPress += OnKeyPress;
}

private void OnKeyPress(object sender, KeyEventArgs e)
{
        Keycode code = e.KeyCode;
        // How to convert to char?
}

How do i convert the Keycode to a char? For instance:

"Space" -> " "
"Enter" -> "\n"
"A"     -> "A"

Upvotes: 2

Views: 1766

Answers (2)

Steffan Pallesen
Steffan Pallesen

Reputation: 550

Based on Vaikesh's answer, this is the solution:

private void OnKeyPress(object sender, KeyEventArgs e)
{
    char c = Convert.ToChar(e.UnicodeChar);
}

Upvotes: 2

Yksh
Yksh

Reputation: 3306

Try something like this :

public override bool OnKeyUp (Keycode keyCode, KeyEvent e)
{
    char keyPressed = (char) e.GetUnicodeChar();
    Log.Debug (string.Format ("On KeyUp is:{0}", keyPressed));
    return base.OnKeyUp (keyCode, e);
}

Upvotes: 2

Related Questions