在.net中捕获Keyboard.KeyDown事件的最简单方法是什么
我正在使用 .Net 4.0 C# 开发一个 Windows 控制台应用程序,用于分析打字模式。
我添加了 PresentationCore
引用来访问 System.Windows.Input.Keyboard
对象。
我应该强调的是,我不仅要尝试捕获按下的按键,还需要计算按下按键的时间。这就是为什么我需要访问 KeyDown
和 KeyUp
事件。
如何实现 KeyDown
和 KeyUp
事件处理程序?
KeyDown
事件只能从应用程序的上下文中记录。
这是我尝试过的代码:(请注意,我陷入了分配处理程序的困境)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
namespace TypingBiometrics
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Type this sentence");
Console.ReadLine();
Keyboard.KeyDownEvent += new KeyboardEventHandler(/*not sure here*/);
}
public void KeyDown(Object sender, KeyboardEventArgs e)
{
Console.WriteLine(e.ToString());
}
}
}
I am developing a Windows Console application in .Net 4.0 C# that analyzes typing patterns.
I've added the PresentationCore
reference to gain access to System.Windows.Input.Keyboard
object.
I should stress that I'm not only trying to capture the key pressed, I need to calculate the amount of time the key was pressed. That is why I need to access the the KeyDown
and KeyUp
events.
How do I implement a KeyDown
and a KeyUp
event handler?
KeyDown
events should only be recorded from the context of the application.
This is the code I've tried: (notice I'm stuck at assigning a handler)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
namespace TypingBiometrics
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Type this sentence");
Console.ReadLine();
Keyboard.KeyDownEvent += new KeyboardEventHandler(/*not sure here*/);
}
public void KeyDown(Object sender, KeyboardEventArgs e)
{
Console.WriteLine(e.ToString());
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
KeyDown
应该足够了。但是,您需要将该函数标记为静态。KeyDown
should be sufficient. However you will need to mark the function as static.