Beispiel #1
0
 private void button1_Click(object sender, EventArgs e)
 {
     try {
         kHook?.Dispose();
     } catch { }
     try {
         kHook             = new KeyboardHook();
         kHook.KeyPressed += KeyPressed;
         kHook.Emergency  += EmergencyPressed;
     } catch (Win32Exception we) {
         Error("Hook exception", "Windows rejected keyboard hook. Try clicking \"Rehook\"\r\n" + we.Message);
     }
     try {
         mHook?.Dispose();
     } catch { }
     try {
         mHook             = new MouseHook();
         mHook.KeyPressed += MousePressed;
     } catch (Win32Exception we) {
         Error("Hook exception", "Windows rejected mouse hook. Try clicking \"Rehook\"\r\n" + we.Message);
     }
 }
Beispiel #2
0
        public Form1()
        {
            InitializeComponent();
            RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
            object      val = key.GetValue(AppName);

            if (val != null)
            {
                if (val.ToString() == ExePath())
                {
                    checkBox1.Checked = true;
                }
                else
                {
                    key.DeleteValue(AppName);
                }
            }
            notifyIcon1.Icon = SystemIcons.Application;
            names            = new string[] {
                "Escape", "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", "F11",
                "F12", "Tilde", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0",
                "Minus", "Equal", "Backslash", "Backspace", "Tab", "Q", "W", "E", "R", "T", "Y", "U",
                "I", "O", "P", "Left Square", "Right Square", "CapsLock", "A", "S", "D", "F", "G",
                "H", "J", "K", "L", "Seicolon", "Quote", "Left Shift", "Z", "X", "C", "V", "B", "N",
                "M", "Left Angle", "Right Angle", "?", "Right shift", "Left Ctrl", "Left Win", "Space",
                "Right Win", "Apps", "Right Control", "PtrScr", "ScrLock", "Pause", "Insert", "Home",
                "PageUp", "Delete", "End", "Next", "Up", "Left", "Down", "Right", "NumLock",
                "Divide", "Multiply", "Subtract", "NumPad 7", "NumPad 8", "NumPad 9", "Add", "NumPad 4",
                "NumPad 5", "NumPad 6", "NumPad 1", "NumPad 2", "NumPad 3", "Return", "NumPad 0", "Numpad Decimal",
                "Left Alt", "Right Alt"
            };
            keys = new Keys[] {
                Keys.Escape, Keys.F1, Keys.F2, Keys.F3, Keys.F4, Keys.F5, Keys.F6, Keys.F7, Keys.F8, Keys.F9, Keys.F10, Keys.F11,
                Keys.F12, Keys.Oemtilde, Keys.D1, Keys.D2, Keys.D3, Keys.D4, Keys.D5, Keys.D6, Keys.D7, Keys.D8, Keys.D9, Keys.D0,
                Keys.OemMinus, Keys.Oemplus, Keys.Oem5, Keys.Back, Keys.Tab, Keys.Q, Keys.W, Keys.E, Keys.R, Keys.T, Keys.Y, Keys.U,
                Keys.I, Keys.O, Keys.P, Keys.OemOpenBrackets, Keys.Oem6, Keys.Capital, Keys.A, Keys.S, Keys.D, Keys.F, Keys.G,
                Keys.H, Keys.J, Keys.K, Keys.L, Keys.Oem1, Keys.Oem7, Keys.LShiftKey, Keys.Z, Keys.X, Keys.C, Keys.V, Keys.B, Keys.N,
                Keys.M, Keys.Oemcomma, Keys.OemPeriod, Keys.OemQuestion, Keys.RShiftKey, Keys.LControlKey, Keys.LWin, Keys.Space,
                Keys.RWin, Keys.Apps, Keys.RControlKey, Keys.PrintScreen, Keys.Scroll, Keys.Pause, Keys.Insert, Keys.Home,
                Keys.PageUp, Keys.Delete, Keys.End, Keys.Next, Keys.Up, Keys.Left, Keys.Down, Keys.Right, Keys.NumLock,
                Keys.Divide, Keys.Multiply, Keys.Subtract, Keys.NumPad7, Keys.NumPad8, Keys.NumPad9, Keys.Add, Keys.NumPad4,
                Keys.NumPad5, Keys.NumPad6, Keys.NumPad1, Keys.NumPad2, Keys.NumPad3, Keys.Return, Keys.NumPad0, Keys.Decimal,
                Keys.LMenu, Keys.RMenu
            };
            elements = new Key[] {
                kEsc, kF1, kF2, kF3, kF4, kF5, kF6, kF7, kF8, kF9, kF10, kF11, kF12, kTilde, k1, k2, k3, k4, k5, k6, k7, k8, k9, k0, kDMinus,
                kEqual, kBackSlash, kBack, kTab, kQ, kW, kE, kR, kT, kY, kU, kI, kO, kP, kLB, kRB, kCapsLk, kA, kS, kD, kF, kG, kH, kJ, kK, kL,
                kColon, kQuote, kLShift, kZ, kX, kC, kV, kB, kN, kM, kALB, kARB, kSlash, kRShift, kLCtrl, kLWin, kSpace,
                kRWin, kApps, kRCtrl, kPrtScr, kScrLk, kPaus, kIns, kHom, kPgUp, kDel, kEnd, kPgDn, kUp, kLeft, kDown, kRight, kNumLk,
                kDivide, kMultiply, kMinus, kN7, kN8, kN9, kPlus, kN4, kN5, kN6, kN1, kN2, kN3, kNEnter, kN0, kNDot, kLAlt, kRAlt
            };
            topLabels = new[] {
                new Tuple <Label, Label>(lblKey1, lblCount1),
                new Tuple <Label, Label>(lblKey2, lblCount2),
                new Tuple <Label, Label>(lblKey3, lblCount3),
                new Tuple <Label, Label>(lblKey4, lblCount4),
                new Tuple <Label, Label>(lblKey5, lblCount5),
            };
            gradient = new Color[max_percent];
            for (int i = 0; i < max_percent; ++i)
            {
                gradient[i] = Color.FromArgb(255,
                                             (int)(255 - 255 / (float)(max_percent - 1) * i),
                                             (int)(255 - 255 / (float)(max_percent - 1) * i));
            }
            counter = new long[keys.Length];
            if (keys.Length != elements.Length)
            {
                MessageBox.Show(this, "Keys length: " + keys.Length + "\r\nElements length: " + elements.Length);
            }
            comparison = (a, b) => counter[a].CompareTo(counter[b]);
            lastClick  = kEsc;
            try {
                kHook             = new KeyboardHook();
                kHook.KeyPressed += KeyPressed;
                kHook.Emergency  += EmergencyPressed;
            } catch (Win32Exception we) {
                Error("Hook exception", "Windows rejected keyboard hook. Try clicking \"Rehook\"\r\n" + we.Message);
            }
            try {
                mHook             = new MouseHook();
                mHook.KeyPressed += MousePressed;
            } catch (Win32Exception we) {
                Error("Hook exception", "Windows rejected mouse hook. Try clicking \"Rehook\"\r\n" + we.Message);
            }
            LoadCount(DefaultPath);
            SaveCount(DefaultPath);
            UpdateLabels();
            timer1.Start();
        }