I am doing Hot Key registration with KeyBoardHook and using lists with it. I need list to later dispose and unregister the keys

List<Liensberger.KeyboardHook> lButtonKeyBoardHooks; = new List<Liensberger.KeyboardHook>();

This is how I am doing it

void RegisterButtonHotKey(string ProfileName, string ButtonHotKeys, int iButtonNumber)
    {
        Liensberger.KeyboardHook mButtonKey = new Liensberger.KeyboardHook();
        mButtonKey.KeyPressed += new EventHandler<Liensberger.KeyboardHook.KeyPressedEventArgs>((s, e) => mKeyboardHook_KeyPressed(s, e, ProfileName, ButtonHotKeys));
            
            try
        {
            HotKeyFromString.test();
            iButtonNumber = iButtonNumber + 100;
            if (!String.IsNullOrEmpty(ButtonHotKeys))
            {
                mButtonKey.RegisterHotKey(HotKeyFromString.GetModifier(ButtonHotKeys), HotKeyFromString.GetKey(ButtonHotKeys), iButtonNumber);
                    
                }
            lButtonKeyBoardHooks.Add(mButtonKey);                
        }
        catch (Exception ex)
        {
            log.Error("Unable to register hotkey for button " + ButtonHotKeys + ", Button is OFFLINE." + ex.Message);
            
        }
    }

Here keys are being registered

public void RegisterHotKey(ModifierKeys modifier, Keys key, int HotKeyID)
        {
            // increment the counter.
            _currentId = _currentId + 1;

            bool stRegisterationError = true;

            DateTime dt = DateTime.Now + TimeSpan.FromSeconds(20);
            do
            {
                //// register the hot key.
                if (RegisterHotKey(_window.Handle, _currentId, (uint)modifier, (uint)key))
                //if (RegisterHotKey(_window.Handle, HotKeyID, (uint)modifier, (uint)key))
                {
                    dt = DateTime.Now;
                    stRegisterationError = false;
                }
            } while (DateTime.Now < dt);

            if (stRegisterationError) throw new InvalidOperationException("Couldn’t register the hot key.");

            //// register the hot key.
            //if (!RegisterHotKey(_window.Handle, _currentId, (uint)modifier, (uint)key))
            //    throw new InvalidOperationException("Couldn’t register the hot key.");
        }

       

/// <summary>
        /// Un Registers a hot key in the system.
        /// </summary>
        public void UnRegisterHotKey(int HotKeyID)
        {
            //UnregisterHotKey(_window.Handle, HotKeyID);

            if (_currentId > 0)
            {
                for (int i = 0; i <= _currentId; i++)
                {
                    UnregisterHotKey(_window.Handle, i);
                }
            }
        }

I have used Arrays instead of list but result is same.

All this works fine in the debug session and Hot key registration takes 2 to 3 seconds but When I install and deploy the desktop application after creating an installer, It takes more than a minute.

If Instead of List I use individual variables like below then it works fine but I have to use the list

Liensberger.KeyboardHook mKeyboardHook2 = new Liensberger.KeyboardHook();
    Liensberger.KeyboardHook mKeyboardHook3 = new Liensberger.KeyboardHook();

void RegisterbuttonHotKey_Prev(string ProfileName, string ButtonHotKeys, int iButtonNumber)
{
    if (iButtonNumber == 1)
    {

        // Initialize Keyboard Hook
        mKeyboardHook.KeyPressed += new EventHandler<Liensberger.KeyboardHook.KeyPressedEventArgs>((s, e) => mKeyboardHook_KeyPressed(s, e, ProfileName, ButtonHotKeys));
        try
        {
            HotKeyFromString.test();
            iButtonNumber = iButtonNumber + 100;
            if (!String.IsNullOrEmpty(ButtonHotKeys))
            {
                mKeyboardHook.RegisterHotKey(HotKeyFromString.GetModifier(ButtonHotKeys), HotKeyFromString.GetKey(ButtonHotKeys), iButtonNumber);
            }
        }
        catch (Exception ex)
        {
            log.Error("Unable to register hotkey for button " + ButtonHotKeys + ", button is OFFLINE." + ex.Message);
            
        }
    }


    else if (iButtonNumber == 2)
    {

        // Initialize Keyboard Hook
        mKeyboardHook2.KeyPressed += new EventHandler<Liensberger.KeyboardHook.KeyPressedEventArgs>((s, e) => mKeyboardHook_KeyPressed2(s, e, ProfileName, ButtonHotKeys));
        try
        {
            HotKeyFromString.test();
            iButtonNumber = iButtonNumber + 100;
            if (!String.IsNullOrEmpty(ButtonHotKeys))
            {
                mKeyboardHook2.RegisterHotKey(HotKeyFromString.GetModifier(ButtonHotKeys), HotKeyFromString.GetKey(ButtonHotKeys), iButtonNumber);
            }
        }
        catch (Exception ex)
        {
            log.Error("Unable to register hotkey for button " + ButtonHotKeys + ", button is OFFLINE." + ex.Message);
            //MessageBox.Show("Unable to register hotkey for button, button is OFFLINE.", "button Offline", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

    }

    else if (iButtonNumber == 3)
    {

        // Initialize Keyboard Hook
        mKeyboardHook3.KeyPressed += new EventHandler<Liensberger.KeyboardHook.KeyPressedEventArgs>((s, e) => mKeyboardHook_KeyPressed3(s, e, ProfileName, ButtonHotKeys));
        try
        {
            HotKeyFromString.test();
            iButtonNumber = iButtonNumber + 100;
            if (!String.IsNullOrEmpty(ButtonHotKeys))
            {
                mKeyboardHook3.RegisterHotKey(HotKeyFromString.GetModifier(ButtonHotKeys), HotKeyFromString.GetKey(ButtonHotKeys), iButtonNumber);
            }
        }
        catch (Exception ex)
        {
            log.Error("Unable to register hotkey for button " + ButtonHotKeys + ", button is OFFLINE." + ex.Message);
            //MessageBox.Show("Unable to register hotkey for button, button is OFFLINE.", "button Offline", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }


    }
}


void RegisterbuttonHotKey(string ProfileName, string ButtonHotKeys, int iButtonNumber)
{
    Liensberger.KeyboardHook mButtonKey = new Liensberger.KeyboardHook();
    mButtonKey.KeyPressed += new EventHandler<Liensberger.KeyboardHook.KeyPressedEventArgs>((s, e) => mKeyboardHook_KeyPressed(s, e, ProfileName, ButtonHotKeys));
            
            try
    {
        HotKeyFromString.test();
        iButtonNumber = iButtonNumber + 100;
        if (!String.IsNullOrEmpty(ButtonHotKeys))
        {
            mButtonKey.RegisterHotKey(HotKeyFromString.GetModifier(ButtonHotKeys), HotKeyFromString.GetKey(ButtonHotKeys), iButtonNumber);
                    
                }
        lButtonKeyBoardHooks.Add(mButtonKey);                
    }
    catch (Exception ex)
    {
        log.Error("Unable to register hotkey for button " + ButtonHotKeys + ", Button is OFFLINE." + ex.Message);
        
    }
}

void mKeyboardHook_KeyPressed(object sender, Liensberger.KeyboardHook.KeyPressedEventArgs e, string ProfileName, string ButtonHotKeys)
{
    if (HotKeyFromString.GetModifier(ButtonHotKeys) == e.Modifier &&
        HotKeyFromString.GetKey(ButtonHotKeys) == e.Key)
    {
        log.Info("USB or Button HotKey is Pressed.");

        Alertus.DesktopUI.FormCommonCode.buttonPressed(_ButtonManager, "USB or Keyboard HotKey", ProfileName);
    }
}
void mKeyboardHook_KeyPressed2(object sender, Liensberger.KeyboardHook.KeyPressedEventArgs e, string ProfileName, string ButtonHotKeys)
{
    if (HotKeyFromString.GetModifier(ButtonHotKeys) == e.Modifier &&
        HotKeyFromString.GetKey(ButtonHotKeys) == e.Key)
    {
        log.Info("USB or button HotKey is Pressed.");

        Alertus.DesktopUI.FormCommonCode.buttonPressed(_ButtonManager, "USB or Keyboard HotKey", ProfileName);
    }
}

void mKeyboardHook_KeyPressed3(object sender, Liensberger.KeyboardHook.KeyPressedEventArgs e, string ProfileName, string ButtonHotKeys)
{
    if (HotKeyFromString.GetModifier(ButtonHotKeys) == e.Modifier &&
        HotKeyFromString.GetKey(ButtonHotKeys) == e.Key)
    {
        log.Info("USB or button HotKey is Pressed.");

        Alertus.DesktopUI.FormCommonCode.buttonPressed(_ButtonManager, "USB or Keyboard HotKey", ProfileName);
    }
}

Can any one help me and suggest a solution for this, Thanks.

I tried to use task for this functionality so that hot keys are registered asynchronously and it doesn't slow down the application but it still slows down the app after installing

async void RegisterButtonHotKeys()
    {
        Task<int> registerTask = RegisterButtonHotKeysAsync();
        int result = await registerTask;
    }
    public async Task<int> RegisterButtonHotKeysAsync() 
    {
        await Task.Delay(500); // 500 milli seconds delay
        string Success = "";
        string sButtonProfileName;
        string sButtonHotKeys;
        int iButtonNumber;
        foreach (ButtonProperties p in lButtonPropertiesDetail.ToList())
        {
            sButtonProfileName = p.sButtonProfileName;
            sButtonHotKeys = p.sButtonHotKeys;
            iPanicButtonNumber = p.iButtonNumber;
            Liensberger.KeyboardHook mButtonKey = new Liensberger.KeyboardHook();
            mPanicButtonKey.KeyPressed += new EventHandler<Liensberger.KeyboardHook.KeyPressedEventArgs>((s, e) => mKeyboardHook_KeyPressed(s, e, sButtonProfileName, sButtonHotKeys));                
            try
            {
                HotKeyFromString.test();
                iButtonNumber = iButtonNumber + 100;
                if (!String.IsNullOrEmpty(sButtonHotKeys))
                {
                    mButtonKey.RegisterHotKey(HotKeyFromString.GetModifier(sButtonHotKeys), HotKeyFromString.GetKey(sButtonHotKeys), iButtonNumber);                        
                }
                lButtonKeyBoardHooks.Add(mButtonKey);
                Success = Success + " Key: " + sButtonHotKeys + " Successfully registered ";
            }
            catch (Exception ex)
            {
                Success = Success + " Key: " + sButtonHotKeys + " Failed to register ";
                log.Error("Unable to register hotkey for panic button " + sButtonHotKeys + ", Button is OFFLINE." + ex.Message);
                //MessageBox.Show("Unable to register hotkey for panic button, Button is OFFLINE.", "Button Offline", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        return 1;
    }

but the issue is still there

Basically, the hot keys are not registered and an error is thrown when the installed application tries to register the hot keys for the first time

2023-10-30 17:20:02,357 [1] ERROR - Unable to register hotkey for panic button ctrl+alt+f11, Button is OFFLINE.Couldn’t register the hot key.

2023-10-30 17:20:22,357 [1] ERROR - Unable to register hotkey for panic button alt+ctrl+f12, Button is OFFLINE.Couldn’t register the hot key.

This causes the slowness and when it tries it for the second time, the hot keys are successfully registered. This issue occurs only on installed app. While debugging it works fine.

0

There are 0 answers