.net |

中最简单的方法是捕获Keyboard.KeyDown事件。 我正在.Net 4.0 C#中开发一个Windows控制台应用程序,用于分析键入模式。 我添加了“ 0”引用来访问“ 1”对象。 我要强调的是,我不仅要捕获按下的键,还需要计算按下键的时间。这就是为什么我需要访问
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());
        }
    }
}
    
已邀请:
        
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());
        }
    }
}
    
        
KeyDown
就足够了。但是,您将需要将该功能标记为静态。     

要回复问题请先登录注册