Triying to manage window with hotkeys using SetWindowsHookEx and SetForegroundWindow

90 views Asked by At

im triying to create a tool, to manage some windows, like when i press a key i would change focus to the next window and so on this is the method i wrote to set focus to the next window in my list

public void RoundRobinFunc()
{
    if (indexofCurrentRound == Windows.Count)
    {
        indexofCurrentRound = 0;
        SetForegroundWindow(Windows[indexofCurrentRound].HandlerIDD);
        indexofCurrentRound++;
    }
    else if (indexofCurrentRound < Windows.Count)
        {
        SetForegroundWindow(Windows[indexofCurrentRound].HandlerIDD);
        indexofCurrentRound++;
    }
}

and this is the method i use in my keylog class to execute the above method using hotkeys

private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
    Form1 form1T = System.Windows.Forms.Application.OpenForms.OfType<Form1>().FirstOrDefault();
    if (nCode >= 0 && wParam == (IntPtr)WM_KEYUP)
        {
            int vkCode = Marshal.ReadInt32(lParam);
            var keyName = Enum.GetName(typeof(Keys), vkCode);
            if (form1T.CheckKeys(keyName))
            {
            form1T.RoundRobinFunc();
            }
    }
    return CallNextHookEx(_hookID, nCode, wParam, lParam);
}

the problem is when im debugging everything works perfect, when i press my desire key it would set the next window to be in the foreground, now the problem comes when im using release build, it would get messy, like when i press my desire key, it will switch to the next window once and get stuck there, wont switch to a new window never, its a complete diferent behivour from my debug test, so i dont know how to debug something that works when debugging but wont work outside visual studio.

0

There are 0 answers