0

I want to show Urdu Language characters rather than English chars in Textbox on KeyDown, for example if "b" is typed then Urdu word "ب" should appear on textbox.

I am doing it in WinForm application like following code which is working perfectly, sending English key char to function which returns its Urdu equivalent char and shows in Textbox instead English char.

private void RTBUrdu_KeyPress(object sender, KeyPressEventArgs e)
{
e.KeyChar = AsciiToUrdu(e.KeyChar); //Write Urdu
}

I couldn't find the equivalent of above code in WPF.

Zeeshanef
  • 639
  • 3
  • 14
  • 23

2 Answers2

2

If you can ensure that the language is registered as an input language in the user's system, you can actually do this completely automatically using InputLanguageManager. By setting the attached properties on the textbox, you can effectively change the keyboard input language when the textbox is selected and reset it when it is deselected.

nmclean
  • 7,564
  • 2
  • 28
  • 37
1

Slightly uglier than the WinForms approach, but this should work (using the KeyDown event and the KeyInterop.VirtualKeyFromKey() converter):

private void TextBox_KeyDown(object sender, KeyEventArgs e)
{
    var ch = (char)KeyInterop.VirtualKeyFromKey(e.Key);
    if (!char.IsLetter(ch)) { return; }

    bool upper = false;
    if (Keyboard.IsKeyToggled(Key.Capital) || Keyboard.IsKeyToggled(Key.CapsLock))
    {
        upper = !upper;
    }
    if (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift))
    {
        upper = !upper;
    }
    if (!upper)
    {
        ch = char.ToLower(ch);
    }

    var box = (sender as TextBox);
    var text = box.Text;
    var caret = box.CaretIndex;

    //string urdu = AsciiToUrdu(e.Key);
    string urdu = AsciiToUrdu(ch);

    //Update the TextBox' text..
    box.Text = text.Insert(caret, urdu);
    //..move the caret accordingly..
    box.CaretIndex = caret + urdu.Length;
    //..and make sure the keystroke isn't handled again by the TextBox itself:
    e.Handled = true;
}
Sphinxxx
  • 12,484
  • 4
  • 54
  • 84
  • Thanks for the solution. But e.Key always returns Capital Key for example if small _a_ is pressed, it will return capital _A_. unlike _e.KeyChar_ with winforms. – Zeeshanef Jul 25 '13 at 22:06
  • I see. I have updated my example to include some basic upper/lower-case checking (and `AsciiToUrdu` now gets a `char` instead of the `Key` enum). To handle even more advanced scenarios, you might need to do some win32 interop calls. See this post for an example: [how to capture the '#' character on different locale keyboards in WPF/C#?](http://stackoverflow.com/questions/5825820/how-to-capture-the-character-on-different-locale-keyboards-in-wpf-c) – Sphinxxx Jul 25 '13 at 23:13
  • you solution was working but there was one problem, because Urdu writing direction is right to left but textbox caret was still in Left-to-Right direction. to solve this, I put Urdu Textbox into Grid and make Grid's FlowDirection Property to RightToLeft. now its is working 100% ok. Thanks – Zeeshanef Jul 31 '13 at 22:29