Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Runtime.InteropServices;
- using System.Diagnostics;
- using swf = System.Windows.Forms;
- namespace Capslator
- {
- class Program
- {
- static IntPtr _hookID = IntPtr.Zero;
- static HookProc _proc = HookCallback;
- const int WM_INPUTLANGCHANGEREQUEST = 0x050;
- const int INPUTLANGCHANGE_FORWARD = 0x0002;
- const int WM_KEYDOWN = 0x0100;
- const int WH_KEYBOARD_LL = 0x000D;
- static void Main(string[] args)
- {
- _hookID = SetHook(HookCallback);
- swf::Application.Run();
- UnhookWindowsHookEx(_hookID);
- }
- private static IntPtr SetHook(HookProc proc)
- {
- using (Process curProcess = Process.GetCurrentProcess())
- using (ProcessModule curModule = curProcess.MainModule)
- {
- return SetWindowsHookEx(WH_KEYBOARD_LL, proc, GetModuleHandle(curModule.ModuleName), 0);
- }
- }
- private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
- {
- if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
- {
- if (swf::Keys.CapsLock == (swf::Keys)Marshal.ReadInt32(lParam))
- {
- PostMessage(GetForegroundWindow(), WM_INPUTLANGCHANGEREQUEST, INPUTLANGCHANGE_FORWARD, 0);
- return (IntPtr)1;
- }
- }
- return CallNextHookEx(_hookID, nCode, wParam, lParam);
- }
- [DllImport("user32.dll")]
- public static extern IntPtr GetForegroundWindow();
- [DllImport("user32.dll", CharSet = CharSet.Auto)]
- public static extern bool PostMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
- [DllImport("user32.dll", SetLastError = true)]
- public static extern IntPtr SetWindowsHookEx(int hookType, HookProc lpfn, IntPtr hMod, uint dwThreadId);
- [DllImport("user32.dll")]
- public static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
- [DllImport("user32.dll", SetLastError = true)]
- [return: MarshalAs(UnmanagedType.Bool)]
- public static extern bool UnhookWindowsHookEx(IntPtr hhk);
- [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
- public static extern IntPtr GetModuleHandle(string lpModuleName);
- public delegate IntPtr HookProc(int code, IntPtr wParam, IntPtr lParam);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement