InterceptKeys() { keyStream = Observable.Create<InterceptKeyEventArgs>(observer => { Debug.Write("Subscribed to keys"); IntPtr hookId = IntPtr.Zero; // Need to hold onto this callback, otherwise it will get GC'd as it is an unmanged callback callback = (nCode, wParam, lParam) => { if (nCode >= 0) { var eventArgs = CreateEventArgs(wParam, lParam); observer.OnNext(eventArgs); if (eventArgs.Handled) return (IntPtr)1; } // ReSharper disable once AccessToModifiedClosure return Win32Methods.CallNextHookEx(hookId, nCode, wParam, lParam); }; hookId = SetHook(callback); return Disposable.Create(() => { Debug.Write("Unsubscribed from keys"); Win32Methods.UnhookWindowsHookEx(hookId); callback = null; }); }) .Publish().RefCount(); }
InterceptKeys() { keyStream = Observable.Create <InterceptKeyEventArgs>(observer => { Debug.Write("Subscribed to keys"); IntPtr hookId = IntPtr.Zero; // Need to hold onto this callback, otherwise it will get GC'd as it is an unmanged callback callback = (nCode, wParam, lParam) => { if (nCode >= 0) { var eventArgs = CreateEventArgs(wParam, lParam); observer.OnNext(eventArgs); if (eventArgs.Handled) { return((IntPtr)1); } } // ReSharper disable once AccessToModifiedClosure return(Win32Methods.CallNextHookEx(hookId, nCode, wParam, lParam)); }; hookId = SetHook(callback); return(Disposable.Create(() => { Debug.Write("Unsubscribed from keys"); Win32Methods.UnhookWindowsHookEx(hookId); callback = null; })); }) .Publish().RefCount(); }
static IntPtr SetHook(Win32Methods.LowLevelKeyboardProc proc) { //TODO: This requires FullTrust to use the Process class - is there any options for doing this in MediumTrust? // using (Process curProcess = Process.GetCurrentProcess()) using (ProcessModule curModule = curProcess.MainModule) { return(Win32Methods.SetWindowsHookEx(Win32Methods.WH_KEYBOARD_LL, proc, Win32Methods.GetModuleHandle(curModule.ModuleName), 0)); } }
InterceptKeys(IDesktopLockEventService desktopLockEventService) { if (desktopLockEventService == null) { throw new ArgumentNullException("desktopLockEventService"); } subject = new Subject <InterceptKeyEventArgs>(); callback = HookCallback; this.desktopLockEventService = desktopLockEventService; this.desktopLockEventService.DesktopUnlockedEvent += DesktopLockEventServiceDesktopUnlockEvent; this.desktopLockEventService.DesktopUnlockedEvent += DesktopLockEventServiceDesktopLockEvent; }
static IntPtr SetHook(Win32Methods.LowLevelKeyboardProc proc) { // NOTE: This requires FullTrust to use the Process class. // There don't seem to be alternatives to achieving this in // MediumTrust environment which is fine because that's a // concept that has long been obsoleted. But just a warning // if you ever try and run Carnac in that sort of way. using (Process curProcess = Process.GetCurrentProcess()) using (ProcessModule curModule = curProcess.MainModule) { return(Win32Methods.SetWindowsHookEx(Win32Methods.WH_KEYBOARD_LL, proc, Win32Methods.GetModuleHandle(curModule.ModuleName), 0)); } }
InterceptKeys() { subject = new Subject <InterceptKeyEventArgs>(); callback = HookCallback; }