Search code examples
c#silverlightarrow-keys

How to make the arrows keys act as another keys?


In Silverlight project, how to make the left arrow act like . (dot), when a user press the left arrow in a textbox it will type . and also in the same way how to make the right arrow act like - ( dash)

And I want to use the CTRL key to switch between 2 modes: . and dash, regular arrows behavior, mean when a user press Control the tow arrows will act as . and dash. And when a user press agian the control the 2 arrows will act as usual arrows.


Solution

  • private void textBox1_KeyUp(object sender, KeyEventArgs e)
            {
                if (sender is TextBox)
                {
                    TextBox textBox = (TextBox)sender; 
                    if (e.Key == Key.Left || e.Key == Key.Right)
                    {
                        e.Handled = true; 
                        char insert; 
                        if (e.Key == Key.Left) 
                        { 
                            textBox1.SelectionStart = textBox1.Text.Length + 1; 
                            insert = '.';
                        }
                        else
                        { 
                            insert = '-';
                        } 
                        int i = textBox.SelectionStart;
                        textBox1.Text = textBox1.Text.Insert(i, insert.ToString());
                        textBox1.Select(i + 1, 0);
                    }
                }
            }