通过WPF中的接口引发键盘事件 [英] Raising Keyboard events via an Interface in WPF
问题描述
我试图在WPF中构建一个应用程序,在这里我想处理连接到单台计算机的多个键盘的输入(KeyPress事件).因此,我只有一个类,可以覆盖 WndProc()方法,并且能够接收来自不同键盘设备的输入.在执行此操作时,我在MainWindow句柄中注册了此类,因此窗口可以接收CLR键盘事件.但这不是问题.
I am trying to build an application in WPF, where I want to handle inputs (KeyPress events) from multiple Keyboards connected to a single computer. So, I have a single class where I override the WndProc() method and am able to receive input from different keyboard devices. While doing this, I register this class with the MainWindow handle and the window can thus receive the CLR keyboard events. But this is not a problem.
问题是,现在我正在WPF中开发UserControl
,它也可以响应这些多键盘事件.这些UserControls
将在同一窗口中实例化,但是我正在考虑通过接口发送事件(尤其是RoutedEvents
).因此,我的自定义UserControl类仅实现了该接口,我很高兴.
The problem is that now I am developing UserControl
in WPF which can also respond to these multi-keyboard events. These UserControls
will be instantiated in the same window, but I was thinking of sending the events (especially as RoutedEvents
) via an interface. So that, my custom UserControl class simply implements the interface and I am good to go.
你们是否知道如何执行此操作,我真的是WPF的新手,所以遇到了一些困难.
Do you guys have any idea how to do this, I am really new to WPF so I am having some difficulties.
提前感谢!!!
推荐答案
因此,我弄清楚了如何做到这一点.我有一个名为BIFUserControl
的类,该类实现了我创建的用于处理连接到同一台计算机的多个键盘上的事件的接口.该接口称为IMultiKeyboardEvents
.我还有一个只读集合,该集合将每个键盘设备的当前状态存储为BIFKeyboardDevice
类的实例.
So, I figured out how to do this. I have a class called BIFUserControl
which implements the interface I have created to handle events from multiple keyboards attached to the same computer. The interface is called IMultiKeyboardEvents
. I also have a read-only collection which stores the current state of each keyboard device as instances of BIFKeyboardDevice
class.
因此,基本上,我知道每个键盘设备的输入焦点是什么,它表示为BIFKeyboardDevice
类中的一个属性.因此,当从键盘管理器类BIFKeyboardManager
引发CLR事件时,我实际上可以在确切的类中调用接口方法实现,该类的输入焦点集中在特定控件上.
So, basically I know what is the input focus for each keyboard device, which is represented as a property in the BIFKeyboardDevice
class. So when raising a CLR event from Keyboard manager class BIFKeyboardManager
I can actually call the interface method implementation in the exact class which has the input focus to a specific control.
这是接口IMultiKeyboardEvents
的代码,该代码由自定义用户控件类实现:
Here is the code for the interface IMultiKeyboardEvents
which is implemented by the custom user control class:
public interface IMultiKeyboardEvents
{
event MultiKeyEventHandler MultiKeyDown;
event MultiKeyEventHandler MultiKeyUp;
void OnMultiKeyDown(MultiKeyEventArgs);
void OnMultiKeyDown(MultiKeyEventArgs);
}
在覆盖 WndProc()方法的键盘管理器类中,我有一个处理输入事件的方法,如下所示:
And in the keyboard manager class which overrides the WndProc() method, I have a method which processes the input events which is as follows :
void ProcessInput(Message message)
{
// Code sample which raises just the key down event
switch (rawInput.keyboard.Message)
{
case (uint) BIFWin32.WindowMessage.WM_KEYDOWN:
{
if (BIFDeviceCollections.MouseCollection[ID].MouseFocusElement is IMultiKeyboardEvents)
{
IMultiKeyboardEvents widget =
(IMultiKeyboardEvent)BIFDeviceCollections.MouseCollection[ID].MouseFocusedElement;
widget.OnMultiKeyDown(eventArgs);
}
break;
}
case (uint) BIFWin32.WindowMessage.WM_KEYUP:
{
if (BIFDeviceCollections.MouseCollection[ID].MouseFocusElement is IMultiKeyboardEvents)
{
IMultiKeyboardEvents widget =
(IMultiKeyboardEvent)BIFDeviceCollections.MouseCollection[ID].MouseFocusedElement;
widget.OnMultiKeyUp(eventArgs);
}
break;
}
}
}
这只是在我的 UserControl 类中调用OnMultiKeyDown方法的实现,而我不必在用户控件类中使用键盘管理器实例并连接事件以侦听它们.另外,由于我已经收集了所有设备对象(鼠标和键盘),并且它们维护着一个字段,说明每个设备当前的焦点元素是什么,而且我已经将每个鼠标和键盘设备配对了,因此可以直接使用它引发事件的信息.
This simply calls the implementation of OnMultiKeyDown method in my UserControl class and I don't have to use use the keyboard manager instance in the user control class and hook up the events to listen to them. Also, as I have already a collection of all the device objects (mice and keyboards) and they maintain a field saying what is the current focus element for each device and also I have paired each mouse and keyboard device, so I can directly use this information to raise the events.
这篇关于通过WPF中的接口引发键盘事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!