private static void KeyPressToList(Keys key, bool isKeyUp) { var selectedKey = PressedKeyCounts.FirstOrDefault(k => k.Key.Equals(key)); // Create the key counter if it doesn't exist if (selectedKey == default(KeyCount)) { selectedKey = new KeyCount(key); PressedKeyCounts.Add(selectedKey); } if (isKeyUp) { selectedKey.KeyUpCounter++; } else { selectedKey.KeyDownCounter++; } }
public static void Main() { // Set up the program PressedKeyCounts = new List <KeyCount>(); if (!Directory.Exists(LoggingDir)) { Directory.CreateDirectory(LoggingDir); } // Load from existing file if (File.Exists(LoggingDir + FileName)) { try { using (var reader = new StreamReader(File.OpenRead(LoggingDir + FileName))) { while (!reader.EndOfStream) { string line = reader.ReadLine(); var itemToAdd = new KeyCount(line.Split(',')); PressedKeyCounts.Add(itemToAdd); } } } catch (Exception ex) { Console.WriteLine(ex.Message); Console.ReadKey(); return; // Stop code execution } } // Start the save timer SaveTimer.Elapsed += SaveTimer_Elapsed; SaveTimer.AutoReset = true; SaveTimer.Enabled = true; // Run the main function _hookID = SetHook(_proc); Application.Run(); UnhookWindowsHookEx(_hookID); }