如何使箭头键作为另一个键?

时间:2010-04-11 09:00:04

标签: c# silverlight arrow-keys

在Silverlight项目中,当用户按下文本框中的左箭头时,如何使左箭头表现为(点)。并且以同样的方式如何使右箭头像 - (破折号)

我想使用CTRL键在两种模式之间切换:。和破折号,常规箭头行为,意味着当用户按下控制时,牵引箭头将充当。和破折号。当用户按下控件时,2个箭头将像通常的箭头一样。

2 个答案:

答案 0 :(得分:2)

如果是win表单或WPF,你只需捕获keypressed事件并更改它的行为,然后将其设置为“Handled”(前后有一堆事件(PreviewKeyDown)可用于完全控制每次按键都会发生。

您可以使用API​​检查是否按下了CTRL键。 在WPF中使用KeyboardDevice属性,检查:

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

增加: 同时 - 请查看SO question

还有这个:SO Question2

答案 1 :(得分:0)

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);
                }
            }
        }
相关问题