Exemple #1
0
        public KeybindingService(ILoggerFactory loggerFactory,
                                 IOptions <SoundboardOptions> options,
                                 IOptionsMonitor <SoundboardOptions> optionsMonitor)
        {
            _options        = options;
            _optionsMonitor = optionsMonitor;
            _optionsMonitor.OnChange((cfg, _) => LoadBindings(cfg));
            _logger            = loggerFactory.CreateLogger <KeybindingService>();
            _bindings          = new List <KeyBinding>();
            _keys              = new HashSet <int>();
            _lock              = new ReaderWriterLockSlim();
            _reset             = new AutoResetEvent(false);
            _thread            = new Thread(ProcessBindingActivations);
            _cancellationToken = new CancellationTokenSource();
            _thread.Start();

            LoadBindings(_options.Value);

            using (var process = Process.GetCurrentProcess())
                using (var processModule = process.MainModule)
                {
                    _hookProc = OnKeyboardEvent;
                    _hook     = Win32.SetWindowsHookEx(Win32.WH_KEYBOARD_LL, _hookProc,
                                                       Win32.GetModuleHandle(processModule.ModuleName), 0);
                }
        }
Exemple #2
0
        /// <summary> 
        /// 安装勾子 
        /// </summary> 
        /// <param name="hookProcess">外部调用的键盘处理事件</param> 
        public void InstallHook(ProcessKeyHandle clientMethod)
        {
            _clientMethod = clientMethod;

            // 安装键盘钩子
            if (_hHookValue == 0) {
                _KeyBoardHookProcedure = new Win32.HookHandle(OnHookProc);

                _hookWindowPtr = Win32.GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName);

                _hHookValue = Win32.SetWindowsHookEx(
                WH_KEYBOARD_LL,
                _KeyBoardHookProcedure,
                _hookWindowPtr,
                0);

                //如果设置钩子失败.
                if (_hHookValue == 0) UninstallHook();
            }
        }