如何在没有按下输入C#WPF的情况下检测我的键盘

时间:2017-12-25 16:55:32

标签: c# wpf keyboard key detection

如何将此代码更改为我的键盘的主动检测。现在它显示我按下输入后写的内容。如何在不输入密钥的情况下显示我可以写的内容。

XAML:

<StackPanel>
  <TextBlock Width="300" Height="20">
   Type some text into the TextBox and press the Enter key.
  </TextBlock>
  <TextBox Width="300" Height="30" Name="textBox1"
           KeyDown="OnKeyDownHandler"/>
  <TextBlock Width="300" Height="100" Name="textBlock1"/>
</StackPanel>

C#:

private void OnKeyDownHandler(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Return)
    {
        textBlock1.Text = "You Entered: " + textBox1.Text;
    }
}

或者也许是创造它的一些不同方式?

3 个答案:

答案 0 :(得分:1)

您可以直接绑定文本:

<StackPanel>
  <TextBlock Width="300" Height="20">
   Type some text into the TextBox and it will appear in the field automatically.
  </TextBlock>
  <TextBox Width="300" Height="30" Name="textBox1" />
  <TextBlock Width="300" Height="100" Name="textBlock1" Text="{Binding Text, ElementName=textbox1}"/>
</StackPanel>

这样您就不需要任何代码隐藏。

修改

如果你想要更复杂的东西,试试这个。在项目中实现一个新类,如下所示:

public class MyConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return $"You entered: {value ?? "nothing"}";
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}

然后将绑定更改为

<Window.Resources>
    <local:MyConverter x:Key="MyConverter"/>
</Window.Resources>
<StackPanel>
    <TextBox Name="txtEdit" />
    <TextBlock Text="{Binding Text, Converter={StaticResource MyConverter}, ElementName=txtEdit}" />
</StackPanel>

不要忘记窗口的资源。

以下是一个屏幕视频,显示了它的实际效果:

Screen-Video

答案 1 :(得分:1)

..\external "vendor_c0cb30d4f33754762565":1 Uncaught ReferenceError: vendor_c0cb30d4f33754762565 is not defined
    at Object.dll-reference vendor_c0cb30d4f33754762565 (..\external "vendor_c0cb30d4f33754762565":1)
    at __webpack_require__ (bootstrap ab071247d46d1a97c83e:678)
    at fn (bootstrap ab071247d46d1a97c83e:88)
    at Object../node_modules/webpack/buildin/global.js (global.js from dll-reference vendor_c0cb30d4f33754762565:1)
    at __webpack_require__ (bootstrap ab071247d46d1a97c83e:678)
    at fn (bootstrap ab071247d46d1a97c83e:88)
    at Object../node_modules/punycode/punycode.js (punycode.js:533)
    at __webpack_require__ (bootstrap ab071247d46d1a97c83e:678)
    at fn (bootstrap ab071247d46d1a97c83e:88)
    at Object../node_modules/url/url.js (url.js:24)

不要使用直接控制属性,而使用 MVVM 和绑定。

“绑定的UpdateSourceTrigger属性控制将更改的值发送回源的方式和时间。”

http://www.wpf-tutorial.com/data-binding/the-update-source-trigger-property/

答案 2 :(得分:1)

如果我正确理解了这个问题,您需要挖掘PreviewKeyDown事件:

private void OnPreviewKeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.G)
    {
        e.Handled = true;
    }
}

或者,您可以使用Keyboard课程。实际上,Keyboard类可以在代码中任何地方使用

private void SomeMethod()
{
    if (Keyboard.IsKeyDown(Key.LeftCtrl))
    {
        MessageBox.Show("Release left Ctrl button");
        return;
    }
    //Do other work
}