在.net中捕获Keyboard.KeyDown事件的最简单方法是什么

发布于 2024-11-11 02:30:40 字数 988 浏览 4 评论 0原文

我正在使用 .Net 4.0 C# 开发一个 Windows 控制台应用程序,用于分析打字模式。

我添加了 PresentationCore 引用来访问 System.Windows.Input.Keyboard 对象。

我应该强调的是,我不仅要尝试捕获按下的按键,还需要计算按下按键的时间。这就是为什么我需要访问 KeyDownKeyUp 事件。

如何实现 KeyDownKeyUp 事件处理程序?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忆离笙 2024-11-18 02:30:40

KeyDown 应该足够了。但是,您需要将该函数标记为静态。

KeyDown should be sufficient. However you will need to mark the function as static.

喜爱皱眉﹌ 2024-11-18 02:30:40
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public class Form1 : Form
    {
        DateTime keyDownTime;
        DateTime keyUpTime;


        public Form1()
        {
            this.SuspendLayout();
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(284, 262);
            this.Name = "Form1";
            this.Text = "Form1";
            this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
            this.KeyUp += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyUp);
            this.ResumeLayout(false);
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            keyDownTime = DateTime.Now;
        }

        private void Form1_KeyUp(object sender, KeyEventArgs e)
        {
            keyUpTime = DateTime.Now;

            MessageBox.Show((keyUpTime.Subtract(keyDownTime)).TotalSeconds.ToString());
        }
    }
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public class Form1 : Form
    {
        DateTime keyDownTime;
        DateTime keyUpTime;


        public Form1()
        {
            this.SuspendLayout();
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(284, 262);
            this.Name = "Form1";
            this.Text = "Form1";
            this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
            this.KeyUp += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyUp);
            this.ResumeLayout(false);
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            keyDownTime = DateTime.Now;
        }

        private void Form1_KeyUp(object sender, KeyEventArgs e)
        {
            keyUpTime = DateTime.Now;

            MessageBox.Show((keyUpTime.Subtract(keyDownTime)).TotalSeconds.ToString());
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文