Advertisement
ZrnecX

Untitled

May 1st, 2017
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 14.73 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Runtime.InteropServices;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9.  
  10. namespace KeyMon
  11. {
  12.     public class LowLevelKeyMonitor
  13.     {
  14.  
  15.         #region WinAPI Constants
  16.         public enum MouseMessages
  17.         {
  18.             WM_MOUSEMOVE = 0x0200,
  19.             WM_MOUSEWHEEL = 0x020A,
  20.         }
  21.  
  22.         public enum VKeys
  23.         {
  24.             // Losely based on http://www.pinvoke.net/default.aspx/Enums/VK.html
  25.            
  26.             // LBUTTON = 0x01,     // Left mouse button
  27.             // RBUTTON = 0x02,     // Right mouse button
  28.             CANCEL = 0x03,      // Control-break processing
  29.             MBUTTON = 0x04,     // Middle mouse button (three-button mouse)
  30.             XBUTTON1 = 0x05,    // Windows 2000/XP: X1 mouse button
  31.             XBUTTON2 = 0x06,    // Windows 2000/XP: X2 mouse button
  32.             //                  0x07   // Undefined
  33.             BACK = 0x08,        // BACKSPACE key
  34.             TAB = 0x09,         // TAB key
  35.             //                  0x0A-0x0B,  // Reserved
  36.             CLEAR = 0x0C,       // CLEAR key
  37.             RETURN = 0x0D,      // ENTER key
  38.             //                  0x0E-0x0F, // Undefined
  39.             SHIFT = 0x10,       // SHIFT key
  40.             CONTROL = 0x11,     // CTRL key
  41.             MENU = 0x12,        // ALT key
  42.             PAUSE = 0x13,       // PAUSE key
  43.             CAPITAL = 0x14,     // CAPS LOCK key
  44.             KANA = 0x15,        // Input Method Editor (IME) Kana mode
  45.             HANGUL = 0x15,      // IME Hangul mode
  46.             //                  0x16,  // Undefined
  47.             JUNJA = 0x17,       // IME Junja mode
  48.             FINAL = 0x18,       // IME final mode
  49.             HANJA = 0x19,       // IME Hanja mode
  50.             KANJI = 0x19,       // IME Kanji mode
  51.             //                  0x1A,  // Undefined
  52.             ESCAPE = 0x1B,      // ESC key
  53.             CONVERT = 0x1C,     // IME convert
  54.             NONCONVERT = 0x1D,  // IME nonconvert
  55.             ACCEPT = 0x1E,      // IME accept
  56.             MODECHANGE = 0x1F,  // IME mode change request
  57.             SPACE = 0x20,       // SPACEBAR
  58.             PRIOR = 0x21,       // PAGE UP key
  59.             NEXT = 0x22,        // PAGE DOWN key
  60.             END = 0x23,         // END key
  61.             HOME = 0x24,        // HOME key
  62.             LEFT = 0x25,        // LEFT ARROW key
  63.             UP = 0x26,          // UP ARROW key
  64.             RIGHT = 0x27,       // RIGHT ARROW key
  65.             DOWN = 0x28,        // DOWN ARROW key
  66.             SELECT = 0x29,      // SELECT key
  67.             PRINT = 0x2A,       // PRINT key
  68.             EXECUTE = 0x2B,     // EXECUTE key
  69.             SNAPSHOT = 0x2C,    // PRINT SCREEN key
  70.             INSERT = 0x2D,      // INS key
  71.             DELETE = 0x2E,      // DEL key
  72.             HELP = 0x2F,        // HELP key
  73.             KEY_0 = 0x30,       // 0 key
  74.             KEY_1 = 0x31,       // 1 key
  75.             KEY_2 = 0x32,       // 2 key
  76.             KEY_3 = 0x33,       // 3 key
  77.             KEY_4 = 0x34,       // 4 key
  78.             KEY_5 = 0x35,       // 5 key
  79.             KEY_6 = 0x36,       // 6 key
  80.             KEY_7 = 0x37,       // 7 key
  81.             KEY_8 = 0x38,       // 8 key
  82.             KEY_9 = 0x39,       // 9 key
  83.             //                  0x3A-0x40, // Undefined
  84.             KEY_A = 0x41,       // A key
  85.             KEY_B = 0x42,       // B key
  86.             KEY_C = 0x43,       // C key
  87.             KEY_D = 0x44,       // D key
  88.             KEY_E = 0x45,       // E key
  89.             KEY_F = 0x46,       // F key
  90.             KEY_G = 0x47,       // G key
  91.             KEY_H = 0x48,       // H key
  92.             KEY_I = 0x49,       // I key
  93.             KEY_J = 0x4A,       // J key
  94.             KEY_K = 0x4B,       // K key
  95.             KEY_L = 0x4C,       // L key
  96.             KEY_M = 0x4D,       // M key
  97.             KEY_N = 0x4E,       // N key
  98.             KEY_O = 0x4F,       // O key
  99.             KEY_P = 0x50,       // P key
  100.             KEY_Q = 0x51,       // Q key
  101.             KEY_R = 0x52,       // R key
  102.             KEY_S = 0x53,       // S key
  103.             KEY_T = 0x54,       // T key
  104.             KEY_U = 0x55,       // U key
  105.             KEY_V = 0x56,       // V key
  106.             KEY_W = 0x57,       // W key
  107.             KEY_X = 0x58,       // X key
  108.             KEY_Y = 0x59,       // Y key
  109.             KEY_Z = 0x5A,       // Z key
  110.             LWIN = 0x5B,        // Left Windows key (Microsoft Natural keyboard)
  111.             RWIN = 0x5C,        // Right Windows key (Natural keyboard)
  112.             APPS = 0x5D,        // Applications key (Natural keyboard)
  113.             //                  0x5E, // Reserved
  114.             SLEEP = 0x5F,       // Computer Sleep key
  115.             NUMPAD0 = 0x60,     // Numeric keypad 0 key
  116.             NUMPAD1 = 0x61,     // Numeric keypad 1 key
  117.             NUMPAD2 = 0x62,     // Numeric keypad 2 key
  118.             NUMPAD3 = 0x63,     // Numeric keypad 3 key
  119.             NUMPAD4 = 0x64,     // Numeric keypad 4 key
  120.             NUMPAD5 = 0x65,     // Numeric keypad 5 key
  121.             NUMPAD6 = 0x66,     // Numeric keypad 6 key
  122.             NUMPAD7 = 0x67,     // Numeric keypad 7 key
  123.             NUMPAD8 = 0x68,     // Numeric keypad 8 key
  124.             NUMPAD9 = 0x69,     // Numeric keypad 9 key
  125.             MULTIPLY = 0x6A,    // Multiply key
  126.             ADD = 0x6B,         // Add key
  127.             SEPARATOR = 0x6C,   // Separator key
  128.             SUBTRACT = 0x6D,    // Subtract key
  129.             DECIMAL = 0x6E,     // Decimal key
  130.             DIVIDE = 0x6F,      // Divide key
  131.             F1 = 0x70,          // F1 key
  132.             F2 = 0x71,          // F2 key
  133.             F3 = 0x72,          // F3 key
  134.             F4 = 0x73,          // F4 key
  135.             F5 = 0x74,          // F5 key
  136.             F6 = 0x75,          // F6 key
  137.             F7 = 0x76,          // F7 key
  138.             F8 = 0x77,          // F8 key
  139.             F9 = 0x78,          // F9 key
  140.             F10 = 0x79,         // F10 key
  141.             F11 = 0x7A,         // F11 key
  142.             F12 = 0x7B,         // F12 key
  143.             F13 = 0x7C,         // F13 key
  144.             F14 = 0x7D,         // F14 key
  145.             F15 = 0x7E,         // F15 key
  146.             F16 = 0x7F,         // F16 key
  147.             F17 = 0x80,         // F17 key  
  148.             F18 = 0x81,         // F18 key  
  149.             F19 = 0x82,         // F19 key  
  150.             F20 = 0x83,         // F20 key  
  151.             F21 = 0x84,         // F21 key  
  152.             F22 = 0x85,         // F22 key, (PPC only) Key used to lock device.
  153.             F23 = 0x86,         // F23 key  
  154.             F24 = 0x87,         // F24 key  
  155.             //                  0x88-0X8F,  // Unassigned
  156.             NUMLOCK = 0x90,     // NUM LOCK key
  157.             SCROLL = 0x91,      // SCROLL LOCK key
  158.             //                  0x92-0x96,  // OEM specific
  159.             //                  0x97-0x9F,  // Unassigned
  160.             LSHIFT = 0xA0,      // Left SHIFT key
  161.             RSHIFT = 0xA1,      // Right SHIFT key
  162.             LCONTROL = 0xA2,    // Left CONTROL key
  163.             RCONTROL = 0xA3,    // Right CONTROL key
  164.             LMENU = 0xA4,       // Left MENU key
  165.             RMENU = 0xA5,       // Right MENU key
  166.             BROWSER_BACK = 0xA6,    // Windows 2000/XP: Browser Back key
  167.             BROWSER_FORWARD = 0xA7, // Windows 2000/XP: Browser Forward key
  168.             BROWSER_REFRESH = 0xA8, // Windows 2000/XP: Browser Refresh key
  169.             BROWSER_STOP = 0xA9,    // Windows 2000/XP: Browser Stop key
  170.             BROWSER_SEARCH = 0xAA,  // Windows 2000/XP: Browser Search key
  171.             BROWSER_FAVORITES = 0xAB,  // Windows 2000/XP: Browser Favorites key
  172.             BROWSER_HOME = 0xAC,    // Windows 2000/XP: Browser Start and Home key
  173.             VOLUME_MUTE = 0xAD,     // Windows 2000/XP: Volume Mute key
  174.             VOLUME_DOWN = 0xAE,     // Windows 2000/XP: Volume Down key
  175.             VOLUME_UP = 0xAF,  // Windows 2000/XP: Volume Up key
  176.             MEDIA_NEXT_TRACK = 0xB0,// Windows 2000/XP: Next Track key
  177.             MEDIA_PREV_TRACK = 0xB1,// Windows 2000/XP: Previous Track key
  178.             MEDIA_STOP = 0xB2, // Windows 2000/XP: Stop Media key
  179.             MEDIA_PLAY_PAUSE = 0xB3,// Windows 2000/XP: Play/Pause Media key
  180.             LAUNCH_MAIL = 0xB4,     // Windows 2000/XP: Start Mail key
  181.             LAUNCH_MEDIA_SELECT = 0xB5,  // Windows 2000/XP: Select Media key
  182.             LAUNCH_APP1 = 0xB6,     // Windows 2000/XP: Start Application 1 key
  183.             LAUNCH_APP2 = 0xB7,     // Windows 2000/XP: Start Application 2 key
  184.             //                  0xB8-0xB9,  // Reserved
  185.             OEM_1 = 0xBA,       // Used for miscellaneous characters; it can vary by keyboard.
  186.             // Windows 2000/XP: For the US standard keyboard, the ';:' key
  187.             OEM_PLUS = 0xBB,    // Windows 2000/XP: For any country/region, the '+' key
  188.             OEM_COMMA = 0xBC,   // Windows 2000/XP: For any country/region, the ',' key
  189.             OEM_MINUS = 0xBD,   // Windows 2000/XP: For any country/region, the '-' key
  190.             OEM_PERIOD = 0xBE,  // Windows 2000/XP: For any country/region, the '.' key
  191.             OEM_2 = 0xBF,       // Used for miscellaneous characters; it can vary by keyboard.
  192.             // Windows 2000/XP: For the US standard keyboard, the '/?' key
  193.             OEM_3 = 0xC0,       // Used for miscellaneous characters; it can vary by keyboard.
  194.             // Windows 2000/XP: For the US standard keyboard, the '`~' key
  195.             //                  0xC1-0xD7,  // Reserved
  196.             //                  0xD8-0xDA,  // Unassigned
  197.             OEM_4 = 0xDB,       // Used for miscellaneous characters; it can vary by keyboard.
  198.             // Windows 2000/XP: For the US standard keyboard, the '[{' key
  199.             OEM_5 = 0xDC,       // Used for miscellaneous characters; it can vary by keyboard.
  200.             // Windows 2000/XP: For the US standard keyboard, the '\|' key
  201.             OEM_6 = 0xDD,       // Used for miscellaneous characters; it can vary by keyboard.
  202.             // Windows 2000/XP: For the US standard keyboard, the ']}' key
  203.             OEM_7 = 0xDE,       // Used for miscellaneous characters; it can vary by keyboard.
  204.             // Windows 2000/XP: For the US standard keyboard, the 'single-quote/double-quote' key
  205.             OEM_8 = 0xDF,       // Used for miscellaneous characters; it can vary by keyboard.
  206.             //                  0xE0,  // Reserved
  207.             //                  0xE1,  // OEM specific
  208.             OEM_102 = 0xE2,     // Windows 2000/XP: Either the angle bracket key or the backslash key on the RT 102-key keyboard
  209.             //                  0xE3-E4,  // OEM specific
  210.             PROCESSKEY = 0xE5,  // Windows 95/98/Me, Windows NT 4.0, Windows 2000/XP: IME PROCESS key
  211.             //                  0xE6,  // OEM specific
  212.             PACKET = 0xE7,      // Windows 2000/XP: Used to pass Unicode characters as if they were keystrokes. The VK_PACKET key is the low word of a 32-bit Virtual Key value used for non-keyboard input methods. For more information, see Remark in KEYBDINPUT, SendInput, WM_KEYDOWN, and WM_KEYUP
  213.             //                  0xE8,  // Unassigned
  214.             //                  0xE9-F5,  // OEM specific
  215.             ATTN = 0xF6,        // Attn key
  216.             CRSEL = 0xF7,       // CrSel key
  217.             EXSEL = 0xF8,       // ExSel key
  218.             EREOF = 0xF9,       // Erase EOF key
  219.             PLAY = 0xFA,        // Play key
  220.             ZOOM = 0xFB,        // Zoom key
  221.             NONAME = 0xFC,      // Reserved
  222.             PA1 = 0xFD,         // PA1 key
  223.             OEM_CLEAR = 0xFE    // Clear key
  224.         }
  225.  
  226.         private const int WM_KEYDOWN = 0x100;
  227.         private const int WM_SYSKEYDOWN = 0x104;
  228.         private const int WM_KEYUP = 0x101;
  229.         private const int WM_SYSKEYUP = 0x105;
  230.  
  231.  
  232.         private const int WH_KEYBOARD_LL = 13;
  233.         private const int WH_MOUSE_LL = 14;
  234.  
  235.  
  236.         #endregion
  237.         #region WinAPI
  238.  
  239.         [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  240.         private static extern IntPtr SetWindowsHookEx(int idHook, HookHandler lpfn, IntPtr hMod, uint dwThreadId);
  241.  
  242.         [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  243.         [return: MarshalAs(UnmanagedType.Bool)]
  244.         private static extern bool UnhookWindowsHookEx(IntPtr hhk);
  245.  
  246.         [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  247.         private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
  248.  
  249.         [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  250.         private static extern IntPtr GetModuleHandle(string lpModuleName);
  251.  
  252.    
  253.  
  254.         #endregion
  255.  
  256.         public delegate void KeyCallback(VKeys key);
  257.         public event KeyCallback KeyboardEvent;
  258.         public event KeyCallback MouseEvent;
  259.  
  260.         private delegate IntPtr HookHandler(int nCode, IntPtr wParam, IntPtr lParam);
  261.         private HookHandler k_hookHandler;
  262.         private HookHandler m_hookHandler;
  263.  
  264.         private IntPtr k_hookID = IntPtr.Zero;
  265.         private IntPtr m_hookID = IntPtr.Zero;
  266.        
  267.  
  268.         public LowLevelKeyMonitor()
  269.         {
  270.             k_hookHandler = KeyboardHookFunction;
  271.             m_hookHandler = MouseHookFunction;
  272.  
  273.             k_hookID = SetKeyboardHook(k_hookHandler);
  274.             m_hookID = SetMouseHook(m_hookHandler);
  275.            
  276.         }
  277.  
  278.         public void Die(object sender, EventArgs e)
  279.         {
  280.             UnhookWindowsHookEx(k_hookID);
  281.             UnhookWindowsHookEx(m_hookID);
  282.         }
  283.  
  284.         public IntPtr MouseHookFunction(int nCode, IntPtr wParam, IntPtr lParam)
  285.         {
  286.             File.WriteAllText("KeyMon.log",File.ReadAllText("KeyMon.log")+"\n Mouse Event Fired!");
  287.          
  288.  
  289.             return CallNextHookEx(m_hookID, nCode, wParam, lParam);
  290.         }
  291.  
  292.         public IntPtr KeyboardHookFunction(int nCode, IntPtr wParam, IntPtr lParam)
  293.         {
  294.             File.WriteAllText("KeyMon.log", File.ReadAllText("KeyMon.log") + "\n Keyboard Event Fired!");
  295.          
  296.  
  297.  
  298.             return CallNextHookEx(k_hookID, nCode, wParam, lParam);
  299.         }
  300.  
  301.         private IntPtr SetMouseHook(HookHandler proc)
  302.         {
  303.             using (ProcessModule module = Process.GetCurrentProcess().MainModule)
  304.                 return SetWindowsHookEx(WH_MOUSE_LL, proc, GetModuleHandle(module.ModuleName), 0);
  305.         }
  306.  
  307.         private IntPtr SetKeyboardHook(HookHandler proc)
  308.         {
  309.             using (ProcessModule module = Process.GetCurrentProcess().MainModule)
  310.                 return SetWindowsHookEx(WH_KEYBOARD_LL, proc, GetModuleHandle(module.ModuleName), 0);
  311.         }
  312.     }
  313.  
  314.    
  315. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement