Advertisement
Guest User

Capslator

a guest
Jul 31st, 2011
622
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.45 KB | None | 0 0
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Diagnostics;
  4. using swf = System.Windows.Forms;
  5.  
  6. namespace Capslator
  7. {
  8.     class Program
  9.     {
  10.         static IntPtr _hookID = IntPtr.Zero;
  11.         static HookProc _proc = HookCallback;
  12.         const int WM_INPUTLANGCHANGEREQUEST = 0x050;
  13.         const int INPUTLANGCHANGE_FORWARD = 0x0002;
  14.         const int WM_KEYDOWN = 0x0100;
  15.         const int WH_KEYBOARD_LL = 0x000D;
  16.  
  17.         static void Main(string[] args)
  18.         {
  19.             _hookID = SetHook(HookCallback);
  20.             swf::Application.Run();
  21.             UnhookWindowsHookEx(_hookID);
  22.         }
  23.  
  24.         private static IntPtr SetHook(HookProc proc)
  25.         {
  26.             using (Process curProcess = Process.GetCurrentProcess())
  27.             using (ProcessModule curModule = curProcess.MainModule)
  28.             {
  29.                 return SetWindowsHookEx(WH_KEYBOARD_LL, proc, GetModuleHandle(curModule.ModuleName), 0);
  30.             }
  31.         }
  32.  
  33.         private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
  34.         {
  35.             if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
  36.             {
  37.                 if (swf::Keys.CapsLock == (swf::Keys)Marshal.ReadInt32(lParam))
  38.                 {
  39.                     PostMessage(GetForegroundWindow(), WM_INPUTLANGCHANGEREQUEST, INPUTLANGCHANGE_FORWARD, 0);
  40.                     return (IntPtr)1;
  41.                 }
  42.             }
  43.             return CallNextHookEx(_hookID, nCode, wParam, lParam);
  44.         }
  45.  
  46.         [DllImport("user32.dll")]
  47.         public static extern IntPtr GetForegroundWindow();
  48.  
  49.         [DllImport("user32.dll", CharSet = CharSet.Auto)]
  50.         public static extern bool PostMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
  51.  
  52.         [DllImport("user32.dll", SetLastError = true)]
  53.         public static extern IntPtr SetWindowsHookEx(int hookType, HookProc lpfn, IntPtr hMod, uint dwThreadId);
  54.  
  55.         [DllImport("user32.dll")]
  56.         public static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
  57.  
  58.         [DllImport("user32.dll", SetLastError = true)]
  59.         [return: MarshalAs(UnmanagedType.Bool)]
  60.         public static extern bool UnhookWindowsHookEx(IntPtr hhk);
  61.  
  62.         [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
  63.         public static extern IntPtr GetModuleHandle(string lpModuleName);
  64.  
  65.         public delegate IntPtr HookProc(int code, IntPtr wParam, IntPtr lParam);
  66.     }    
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement