简体   繁体   English

WPF子类捕获按键事件

[英]WPF subclass capturing key press event

I am new to WPF and am building a test application. 我是WPF的新手,正在构建测试应用程序。 In my application I have a user control that is calling another class to capture the fingerprint from the device. 在我的应用程序中,我有一个用户控件,该控件正在调用另一个类以从设备捕获指纹。 The class that captures the fingerprint from the device is running on a do while loop, that constantly keeps reading from the device. 从设备捕获指纹的类在do while循环中运行,该循环持续不断地从设备读取数据。 I want to introduce an event that can break the do while. 我想介绍一个可能会暂时中断的事件。 In my user control I can capture the KeyDown event. 在我的用户控件中,我可以捕获KeyDown事件。 But the class that is capturing the fingerprint is unable to capture the keypress. 但是正在捕获指纹的类无法捕获按键。 Any thing that I am missing on? 我缺少什么?

This is my Sample File.Xaml.cs code

   protected override void OnKeyDown(KeyEventArgs e)
    {
        if (e.Key == Key.LeftCtrl || e.Key == Key.RightCtrl)
        {

        }
        else
            base.OnKeyDown(e);
    }

 private void button_Click(object sender, RoutedEventArgs e)
    {


            TNTFMT220 tntFmt220 = new TNTFMT220();
            string fingerPrintId = "";
            var data = tntFmt220.ContiniousCaptureFingerPrint(ref fingerPrintId);                          

    }

This is my code to capture the fingerprint 这是我捕获指纹的代码

   public bool ContiniousCaptureFingerPrint(ref string FingerPrintScanned)
    {
      do
       {
         //get data from device
        }  while(dataReturned);
     return true;
    }

UserControl is made by a window and a class, when the UserControl window is focused and you press a key, you can get a OnKeyDown event. UserControl由一个窗口和一个类组成,当将UserControl窗口聚焦并按一个键时,可以获得一个OnKeyDown事件。

I think your TNTFMT220 class has not a window, so you can generate the event in UserControl or in Main Window and propagate it to the class in different ways, I suggest you a couple: 我认为您的TNTFMT220类没有窗口,因此您可以在UserControl或主窗口中生成事件,然后以不同的方式将事件传播到该类,建议您几个:

  1. Class Property 类属性

     protected override void OnKeyDown(KeyEventArgs e) { if (e.Key == Key.LeftCtrl || e.Key == Key.RightCtrl) { tntFmt220.doStop= true; } else base.OnKeyDown(e); } private TNTFMT220 tntFmt220; private void button_Click(object sender, RoutedEventArgs e) { tntFmt220 = new TNTFMT220(); string fingerPrintId = ""; var data = tntFmt220.ContiniousCaptureFingerPrint(ref fingerPrintId); } 
  2. Class Method 类方法

     protected override void OnKeyDown(KeyEventArgs e) { if (e.Key == Key.LeftCtrl || e.Key == Key.RightCtrl) { tntFmt220.DoStop(); } else base.OnKeyDown(e); } private TNTFMT220 tntFmt220; private void button_Click(object sender, RoutedEventArgs e) { tntFmt220 = new TNTFMT220(); string fingerPrintId = ""; var data = tntFmt220.ContiniousCaptureFingerPrint(ref fingerPrintId); } 

Above all: 首先:

First of all, I probably think again if that do while is needed or if TNTFMT220 can generate an event when data is read. 首先,我可能会再次考虑是否需do while或者TNTFMT220在读取数据时是否可以生成事件。

Then, if you don't want that do While will block you window, you have to run it in a different thread. 然后,如果您不希望这样do While会阻塞您的窗口,则必须在其他线程中运行它。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM