What can cause Windows to unhook a low level (global) keyboard hook?

后端 未结 7 1755
-上瘾入骨i
-上瘾入骨i 2020-12-13 14:19

We have some global keyboard hooks installed via SetWindowsHookEx with WH_KEYBOARD_LL that appear to randomly get unhooked by Windows.

We

7条回答
  •  伪装坚强ぢ
    2020-12-13 14:42

    I'm using the following project from: http://www.codeproject.com/Articles/7294/Processing-Global-Mouse-and-Keyboard-Hooks-in-C to perform tasks when a certain key has been pressed.

    I noticed that after i performed a task with a hotkey it stopped listening for new keypresses, i then changed the KeyboardHookListener to static and it seemed to solve my problem. I've no idea why this solved my problem, so feel free to comment on this!

    My hotkey class:

     class Hotkey
    {
        private static KeyboardHookListener _keyboardHookListener;
    
        public void Start()
        {
            _keyboardHookListener = new KeyboardHookListener(new GlobalHooker()) { Enabled = true };
            _keyboardHookListener.KeyDown += KeyboardListener_OnkeyPress;
        }
    
        private void KeyboardListener_OnkeyPress(object sender, KeyEventArgs e)
        {
            // Let's backup all projects
            if (e.KeyCode == Keys.F1)
            {
                // Initialize files
                var files = new Files();
    
                // Backup all projects
                files.BackupAllProjects();
            }
            // Quick backup - one project
            else if (e.KeyCode == Keys.F2)
            {
                var quickBackupForm = new QuickBackup();
                quickBackupForm.Show();
            }
        }
    }
    

提交回复
热议问题