Example #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);
                }
        }
Example #2
0
 private static IntPtr SetHook(Win32.LowLevelKeyboardProc proc)
 {
     using (Process curProcess = Process.GetCurrentProcess())
         using (ProcessModule curModule = curProcess.MainModule) {
             return(Win32.SetWindowsHookEx(Win32.WH_KEYBOARD_LL, proc,
                                           Win32.GetModuleHandle(curModule.ModuleName), 0));
         }
 }
Example #3
0
        private IntPtr SetHook()
        {
            this.keyboardProc = new Win32.LowLevelKeyboardProc(this.KeyboardProcessor);

            using (var process = Process.GetCurrentProcess())
            {
                using (var module = process.MainModule)
                {
                    return(Win32.SetWindowsHookEx(Win32.WH_KEYBOARD_LL,
                                                  this.keyboardProc,
                                                  Win32.GetModuleHandle(module.ModuleName), 0));
                }
            }
        }
Example #4
0
        public KeyLoggerAPI()
        {
            HookId         = IntPtr.Zero;
            HookProc       = null;
            KeyPressedProc = null;
            string filePath = GetValidFileName(".", "keylog.txt");

            SWriter = new StreamWriter(filePath, true, encoding: System.Text.Encoding.UTF8);
            SWriter.Write("#!/usr/bin/env subl");
            using (var curProcess = Process.GetCurrentProcess())
                using (var curModule = curProcess.MainModule)
                {
                    CurrentModuleId = Win32.GetModuleHandle(curModule.ModuleName);
                }
            timer = new Timer(OnTimeEventHandler, null, 1, 1000); // 1s
        }
Example #5
0
        private void Dispose(bool disposing)
        {
            try
            {
                _cancellationToken.Cancel();
                _reset.Set();
                _thread.Join();
            }
            catch (Exception)
            {
                // NOTE: Dispose shouldn't throw.
            }
            finally
            {
                _cancellationToken.Dispose();

                if (_hook != null && !_hook.IsInvalid)
                {
                    _hookProc = null;
                    _hook.Dispose();
                }
            }
        }
Example #6
0
        public Program()
        {
            try
            {
                this.keyboard_proc = new Win32.LowLevelKeyboardProc(this.KeyboardProc);
                using (var currentProcess = Process.GetCurrentProcess())
                {
                    using (var mainModule = currentProcess.MainModule)
                    {
                        this.keyboard_hook = Win32.SetWindowsHookEx(WH_KEYBOARD_LL, this.keyboard_proc, Win32.GetModuleHandle(mainModule.ModuleName), 0u);
                    }
                }
            }
            catch
            {
                if (this.keyboard_hook != IntPtr.Zero)
                {
                    Win32.UnhookWindowsHookEx(this.keyboard_hook);
                }

                throw;
            }
        }
Example #7
0
 public void SetHook(Action <KeyPressed> proc)
 {
     KeyPressedProc = proc ?? throw new ArgumentNullException(nameof(proc));
     HookProc       = HookCallback;
     HookId         = Win32.SetWindowsHookEx(WH_KEYBOARD_LL, HookProc, CurrentModuleId, 0);
 }
 public GlobalKbHook()
 {
     _proc = HookCallback;
 }
Example #9
0
 public static extern IntPtr SetWindowsHookEx(int idHook, Win32.LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);
Example #10
0
 public override void Dispose()
 {
     Win32.UnhookWindowsHookEx(this.HookId);
     this.keyboardProc = null;
 }
Example #11
0
 public GlobalKbHook()
 {
     _proc = HookCallback;
 }