views:

142

answers:

3

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.

+2  A: 

If it's win forms or WPF you just catch the event of the keypressed and change it's behavior and than set it as "Handled" (there is a bunch of events before and after (PreviewKeyDown) that you can use to fully control what happens on every key pressing.

You can check if CTRL key is pressed as well using API. using KeyboardDevice Property in WPF, checking:

if ((e.KeyboardDevice.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)

Addition: Meanwhile - check this out SO question

and This one as well: SO Question2

Dani
Thank you Dani, but I forgot to mention that I'm working on silverlight application! how I can do it with Silverlight- I'm new to it!
Sara
I don't know silverligt but I know it is very very similar to wpf.I will check and try to update the answer.
Dani
A: 

public TextBoxKeyPress() { InitializeComponent(); txtInput.KeyUp += new KeyEventHandler(txtInput_KeyUp); txtInput.KeyDown += new KeyEventHandler(txtInput_KeyDown); }

    void txtInput_KeyDown(object sender, KeyEventArgs e)
    {
        s = "";
        switch (e.Key)
        {

            case Key.Left:
                {
                    s = ".";
                    e.Handled = true;
                    break;
                }

            case Key.Right:
                {
                    s = "-";
                    e.Handled = true;
                    break;
                }
        }
    }
    void txtInput_KeyUp(object sender, KeyEventArgs e)
    {
        TextBox txb = (TextBox)sender;

        if (Keyboard.Modifiers == ModifierKeys.Shift) return;
        if (Keyboard.Modifiers == ModifierKeys.Control) return;

        if (txb.SelectedText.Length > 0)
            txb.SelectedText = "";
        switch (e.Key)
        {

            case Key.Left:
                {
                    s = ".";
                    e.Handled = true;
                    break;
                }

            case Key.Right:
                {
                    s = "-";
                    e.Handled = true;
                    break;
                }
        }
        int i = txb.SelectionStart;
        txb.Text = txb.Text.Insert(i, s);
        txb.Select(i + 1, 0);
        e.Handled = true;
    } 

BUT the problem is when I want to write in the textbox .-. actually ..- is written because the arrows do move left/right once and then write . or - , they do the both! how to disable the basic arrows functions ?? any ideas?

Sara
This isn't an answer.
Mark Byers
A: 
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);
                }
            }
        }
Sara