public void RegisterHook(Keys key, KeyboardHookCallback callback)
        {
            Keys modifiers = 0;

            if ((key & Keys.Alt) == Keys.Alt)
            {
                modifiers = modifiers | Keys.Alt;
            }

            if ((key & Keys.Control) == Keys.Control)
            {
                modifiers = modifiers | Keys.Control;
            }

            if ((key & Keys.Shift) == Keys.Shift)
            {
                modifiers = modifiers | Keys.Shift;
            }

            Keys k = key & ~Keys.Control & ~Keys.Shift & ~Keys.Alt;

            if (!callbacks.ContainsKey(modifiers))
            {
                callbacks.Add(modifiers, new Dictionary <Keys, List <KeyboardHookCallback> >());
            }
            if (!callbacks[modifiers].ContainsKey(k))
            {
                callbacks[modifiers].Add(k, new List <KeyboardHookCallback>());
            }

            callbacks[modifiers][k].Add(callback);
        }
Exemplo n.º 2
0
        public Guid RegisterKeyboardHook(KeyboardHookCallback callback)
        {
            var hookId         = default(Guid);
            var hookReadyEvent = new AutoResetEvent(false);
            var hookThread     = new Thread(() =>
            {
                var hook       = new KeyboardHook(callback);
                var sleepEvent = new AutoResetEvent(false);

                hook.Attach();
                hookId = hook.Id;
                KeyboardHooks[hookId] = hook;
                hookReadyEvent.Set();

                while (true)
                {
                    sleepEvent.WaitOne();
                }
            });

            hookThread.SetApartmentState(ApartmentState.STA);
            hookThread.IsBackground = true;
            hookThread.Start();

            hookReadyEvent.WaitOne();

            return(hookId);
        }
Exemplo n.º 3
0
 public IdleNotifier(ISyncContext syncContext, IAppSettingsService settingsService)
 {
     this.syncContext     = syncContext;
     this.settingsService = settingsService;
     keyboardHookCallback = new KeyboardHookCallback(KeyboardHookProc);
     mouseHookCallback    = new MouseHookCallback(MouseHookProc);
     idleTimer            = new Timer(CheckIdleState, null, TIMER_DELAY, TIMER_PERIOD);
 }
Exemplo n.º 4
0
        public void SetHook()
        {
            this.hookCallBack = new KeyboardHookCallback(HookCallback);

            using (Process process = Process.GetCurrentProcess())
            {
                using (ProcessModule module = process.MainModule)
                {
                    this.keyboardHookID = SetWindowsHookEx(WH_KEYBOARD_LL, this.hookCallBack, GetModuleHandle(module.ModuleName), 0);
                }
            }
        }
Exemplo n.º 5
0
        public Form1()
        {
            InitializeComponent();

            // SetWindowsHookExに渡すコールバックメソッドをフィールドで保持しておく
            // フィールドで保持しておかないとガベージコレクション関係のエラーが発生する
            _keyboardHookCallback = KeyCallback;
            _mouseHookCallback    = MouseCallback;

            var hInstance = Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]);

            // SetWindowsHookExでフックを開始します。戻り値はフック解除に必要なのでフィールドで保持しておきます。

            // キーボードフック開始
            _keyboardHookHandle = Native.SetWindowsHookEx(Native.WH_KEYBOARD_LL, _keyboardHookCallback, hInstance, 0);

            // マウスフック開始
            _mouseHookHandle = Native.SetWindowsHookEx(Native.WH_MOUSE_LL, _mouseHookCallback, hInstance, 0);
        }
Exemplo n.º 6
0
        /// <summary>
        /// フック処理をおこないます。
        /// </summary>
        /// <param name="nCode">ウィンドウメッセージなどを受け取ります。</param>
        /// <param name="wParam">パラメータを受け取ります。</param>
        /// <param name="lParam">パラメータを受け取ります。</param>
        /// <returns>CallNextHookEx 関数の戻り値を返します。</returns>
        private IntPtr HookProc(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (nCode >= 0)
            {
                KeyboardHookCallback h = null;

                switch ((User32.WMs)wParam)
                {
                case User32.WMs.WM_KEYDOWN:
                    h = this.KeyDown;
                    break;

                case User32.WMs.WM_SYSKEYDOWN:
                    h = this.KeyDown;
                    break;

                case User32.WMs.WM_KEYUP:
                    h = this.KeyUp;
                    break;

                case User32.WMs.WM_SYSKEYUP:
                    h = this.KeyUp;
                    break;
                }

                if (h != null)
                {
                    if (h((User32.VKs)Marshal.ReadInt32(lParam)))
                    {
                        return(new IntPtr(1));
                    }
                }
            }

            return(CallNextHookEx(this._hookID, nCode, wParam, lParam));
        }
Exemplo n.º 7
0
 private static extern IntPtr SetWindowsHookEx(int idHook, KeyboardHookCallback lpfn, IntPtr hMod, uint dwThreadId);
Exemplo n.º 8
0
 public static extern System.IntPtr SetWindowsHookEx(int idHook, KeyboardHookCallback lpfn, System.IntPtr hMod, uint dwThreadId);
 internal KeyboardHook(KeyboardHookCallback callback)
 {
     this.callback = callback;
     this.Id       = Guid.NewGuid();
 }