简体   繁体   English

如何在不按Enter键的情况下检测键盘C#WPF

[英]How can I detect my keyboard without press enter C# WPF

How can i change this code to active detecion of my keyboard. 如何更改此代码以主动检测键盘。 Now it is showing what i write after press enter. 现在它显示了我按回车后写的内容。 How can i show what i can write without enter key. 我如何显示无需输入键就可以写的内容。

XAML: 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#: C#:

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

Or maybe is some diffrent way to create it? 还是可能有其他创建方式?

You could simply bind the text directly: 您可以直接将文本直接绑定:

<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>

This way you don't need any code-behind. 这样,您不需要任何代码隐藏。

EDIT 编辑

If you want more sophisticated stuff, try this. 如果您想要更复杂的东西,请尝试此。 Implement a new class in your project like this: 在您的项目中实现一个新类,如下所示:

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

and then change your binding to 然后将您的绑定更改为

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

Don't forget the resources for the window. 不要忘记窗口的资源。

Here is a screen video showing it in action: 这是一个屏幕录像,展示了它的运行情况:

屏幕视频

textBlock1.Text = "You Entered: " + **textBox1.Text**;

Don't use direct control property, in contrast use MVVM and binding. 不要使用直接控制属性,相反使用MVVM和绑定。

"The UpdateSourceTrigger property of a binding controls how and when a changed value is sent back to the source." “绑定的UpdateSourceTrigger属性控制如何以及何时将更改的值发送回源。”

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

If I correctly understood the question, you need tunneling PreviewKeyDown event: 如果我正确理解了这个问题,则需要隧道化PreviewKeyDown事件:

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

Alternatively, you can you use Keyboard class. 或者,您可以使用Keyboard类。 In fact, Keyboard class can be used anywhere in your code: 实际上,Keyboard类可以在代码中的任何位置使用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当用户按下输入按钮wpf时,如何在键盘上触发按键事件 - How can I fire a key press event on a keyboard when a user presses enter button wpf C#WPF-如何将文本框的Enter键绑定到方法? - C# WPF - How to Bind TextBox Enter Key Press to a Method? WPF c#检测哪个键盘通过按键发送(以检测它是否是条形码扫描仪) - WPF c# Detect which keyboard sent through a key press (to detect if it was barcode scanner) 如何在C#/ WPF中生成本地化的键盘快捷键? - How can i generate a localized keyboard shortcut in C# / WPF? 如何在WPF / C#中检测鼠标是否在我的窗口范围内移动? - How can i detect if the mouse is moving both within and out of the bounds of my window in WPF/C#? C# WPF 可编辑组合框如何检测回车键 - C# WPF editable Combobox how to detect Enter key 如何在C#中向我的文本框添加键盘快捷键(输入)? - How do I add a keyboard shortcut (enter) to my textbox in C#? 如何删除键盘上的dataGridView行删除键按? C# - How can I delete dataGridView row on keyboard delete key press ? c# 如何通过在C ++和C#中从键盘立即按(不按Enter键)将用户中的数据存储在我的变量中 - How To Store data from the user in my variables by Immediate presses from the keyboard in C++ and C#(without pressing enter) 如何在键盘上按C#执行功能 - How to perform functions on keyboard press c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM