C#钩子-计数键盘上的击键

时间:2018-12-30 16:31:39

标签: c# hook

统计程序。不是keyllogger。对我来说,要知道我的生命是浪费多少时间而无意轻敲键盘;}}

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace StatisticChar
{
    public partial class Form1 : Form
    {
        private GlobalKeyboardHook _globalKeyboardHook;

        public void SetupKeyboardHooks()
        {
            _globalKeyboardHook = new GlobalKeyboardHook();
            _globalKeyboardHook.KeyboardPressed += OnKeyPressed;
        }
        private void OnKeyPressed(object sender, GlobalKeyboardHookEventArgs e)
        {


            if (e.KeyboardState == GlobalKeyboardHook.KeyboardState.KeyDown)
            {
                var znak = $"{(char)e.KeyboardData.VirtualCode} {e.KeyboardData.VirtualCode}";


                //////////////////////    SEKCJA F1   ///////////////////////////////////////////
                if ((e.KeyboardData.VirtualCode >= 112) && (e.KeyboardData.VirtualCode <= 123))
                {
                    znak = "F" + (e.KeyboardData.VirtualCode - 111).ToString();
                }

                //////////////////////   SEKCJA NUMERYCZNA ///////////////////////////////////////////

                if ((e.KeyboardData.VirtualCode >= 96) && (e.KeyboardData.VirtualCode <= 105))
                {
                    znak = (e.KeyboardData.VirtualCode - 96).ToString();
                }


                if (e.KeyboardData.VirtualCode == 144)
                {
                    znak = "NumLock";
                }

                if (e.KeyboardData.VirtualCode == 111)
                {
                    znak = "/";
                }

                if (e.KeyboardData.VirtualCode == 106)
                {
                    znak = "*";
                }

                if (e.KeyboardData.VirtualCode == 109)
                {
                    znak = "-";
                }

                if (e.KeyboardData.VirtualCode == 107)
                {
                    znak = "+";
                }

                if (e.KeyboardData.VirtualCode == 107)
                {
                    znak = ",";
                }

                //////////////////////   SEKCJA GŁÓWNA ///////////////////////////////////////////

                if (e.KeyboardData.VirtualCode == 13)
                {
                    znak = "ENTER";
                }

                if (e.KeyboardData.VirtualCode == 192)
                {
                    znak = "`";
                }

                if (e.KeyboardData.VirtualCode == 9)
                {
                    znak = "tabulator";
                }

                if (e.KeyboardData.VirtualCode == 20)
                {
                    znak = "capslock";
                }

                if (e.KeyboardData.VirtualCode == 160)
                {
                    znak = "LewyShift";
                }

                if (e.KeyboardData.VirtualCode == 162)
                {
                    znak = "ctrl";
                }

                if (e.KeyboardData.VirtualCode == 91)
                {
                    znak = "win";
                }

                if (e.KeyboardData.VirtualCode == 32)
                {
                    znak = "spacja";
                }

                if (e.KeyboardData.VirtualCode == 37)
                {
                    znak = "←";
                }

                if (e.KeyboardData.VirtualCode == 39)
                {
                    znak = "→";
                }

                if (e.KeyboardData.VirtualCode == 38)
                {
                    znak = "↑";
                }

                if (e.KeyboardData.VirtualCode == 40)
                {
                    znak = "↓";
                }

                if (e.KeyboardData.VirtualCode == 8)
                {
                    znak = "backup";
                }

                if (e.KeyboardData.VirtualCode == 161)
                {
                    znak = "prawyShift";
                }

                if (e.KeyboardData.VirtualCode == 93)
                {
                    znak = "ppm";
                }

                if (e.KeyboardData.VirtualCode == 45)
                {
                    znak = "insert";
                }

                if (e.KeyboardData.VirtualCode == 44)
                {
                    znak = "printscreen";
                }

                if (e.KeyboardData.VirtualCode == 19)
                {
                    znak = "pause";
                }

                if (e.KeyboardData.VirtualCode == 166)
                {
                    znak = "back";
                }

                if (e.KeyboardData.VirtualCode == 36)
                {
                    znak = "home";
                }

                if (e.KeyboardData.VirtualCode == 35)
                {
                    znak = "end";
                }

                if (e.KeyboardData.VirtualCode == 46)
                {
                    znak = "delete";
                }

                if (e.KeyboardData.VirtualCode == 33)
                {
                    znak = "pageUp";
                }

                if (e.KeyboardData.VirtualCode == 34)
                {
                    znak = "pagDown";
                }

                if (e.KeyboardData.VirtualCode == 163)
                {
                    znak = "PrawyCtrl";
                }



                //////////////////////   INNE, POZOSTAŁE ///////////////////////////////////////////

                if (e.KeyboardData.VirtualCode == 1)
                {
                    znak = "LeftClick";
                }

                if (e.KeyboardData.VirtualCode == 2)
                {
                    znak = "RightClick";
                }



                Statystyki[znak] = Statystyki.ContainsKey(znak) ? ++Statystyki[znak] : 1;
            }
        }
        public new void Dispose()
        {
            base.Dispose();
            _globalKeyboardHook?.Dispose();
        }


        private DateTime data_zbierania_statystyk;
        private static readonly Dictionary<string, int> Statystyki = new Dictionary<string, int>();

        public Form1()
        {
            _globalKeyboardHook = new GlobalKeyboardHook();
            SetupKeyboardHooks();

            var dt = DateTime.Now;
            data_zbierania_statystyk = new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, 0);

            Timer timer = new Timer
            {
                Interval = 1 * 10 * 1000
            };
            timer.Tick += new EventHandler(Timer_Tick);
            timer.Start();

            Timer timer2 = new Timer
            {
                Interval = 1000
            };
            timer2.Tick += new EventHandler(Timer2_Tick);
            timer2.Start();

            InitializeComponent();

        }

        private void Timer2_Tick(object sender, EventArgs e)
        {
           richTextBox1.Text = StatystykiJakoText();
        }

        void Timer_Tick(object sender, EventArgs e)
        {
            Saves();
        }
        void Saves()
        {
            var dt = DateTime.Now;
            var now = new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, 0);

            if (data_zbierania_statystyk < now)
            {
                data_zbierania_statystyk = now;

                string sb = StatystykiJakoText();

                string path = $@"O:\statystyki{dt.Year}-{dt.Month}-{dt.Day}.txt";

                File.AppendAllText(path, sb);

                richTextBox1.Text = "";
                richTextBox1.Text = sb.ToString();
            }
        }

        private string StatystykiJakoText()
        {
            var sb = new StringBuilder();
            sb.Append(Environment.NewLine);
            sb.Append($"Statystyki za dzień: {data_zbierania_statystyk}{Environment.NewLine}");

            foreach (var literka in Statystyki)
            {
                sb.Append($"{literka.Key} - {literka.Value}{Environment.NewLine}");
            }

            return sb.ToString();
        }


    }
}

但是,仍然存在问题:

  1. 我不会抓到每个字符,例如正确的alt,F模式
  2. 是否无法捕获某些程序(例如记事本++)中编写的字符? 谁能说出这种情况为什么发生以及如何解决?

  3. 如何处理快捷键,例如ctrl + c,ctrl + v?

  4. 如果我们保留它并且不放开它,如何防止按下该键?

1 个答案:

答案 0 :(得分:1)

如果您想要一个完整的键盘挂钩,那么使用这些功能会更有趣:

cat_attr

您一定要捕获所有密钥,并且如果需要,可以轻松阻止某些密钥。

“它是钩子的起源”

您可以这样安装全局挂钩:

attr_de

和使用它的示例:

articles_attr

摘机使用:

JOINs