开发者

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 开发者_StackOverflow中文版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.


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


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);
                }
            }
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜