After check GetKeyState() in main message loop, application freezes until the second press of the same button

Wojciech Sobiesiak 71 Reputation points
2023-02-06T14:52:17.9333333+00:00

Hello, I need to create stop action event in main message loop.(ListBox after press VK_RIGHT moves down - and I don't want it to happen).

Another problem is application freezes until I press right arrow again.

What is happening, why set focus on VK_RIGHT stops widnow from getting messages?


LRESULT SelIndex;
    int rozmiar; 
    HWND hTmp;
   
    while (GetMessage(&msg, nullptr, 0, 0))
    {
        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
        {
            TranslateMessage(&msg);
            
                  
            // this always work 
            if ((HWND)msg.hwnd == hWndExit) { return 0; }
            

            // this never 
            if ((HWND)msg.hwnd == hWndList) { 
              
                SetForegroundWindow(hWndList);  hTmp=SetFocus(hWndList); SendMessage(hWndList, WM_SETFOCUS, (WPARAM)hTmp, 0);
                AttachThreadInput(GetCurrentThreadId(), GetWindowThreadProcessId(GetAncestor(hWndList, GA_ROOT), NULL), TRUE);
                SendMessage(hWndList, WM_UPDATEUISTATE, MAKEWPARAM(UIS_CLEAR, UISF_HIDEFOCUS), 0);

            }
         
            
         if (GetKeyState(VK_RIGHT)) {
               SelIndex = SendMessage(hWndList, LB_GETCURSEL, 0, 0L);
               rozmiar = (int)SendMessage(hWndList, LB_GETCOUNT, 0, 0);
               // SendMessage(hEdit2, WM_SETFOCUS, (WPARAM)hWndList, 0);
              //  hTmp = SetFocus(hEdit2);
               SetForegroundWindow(hEdit2);
                if(SelIndex<rozmiar-1){ SendMessage(hWndList, LB_SETCURSEL, SelIndex-1, 0L); }
                else { SendMessage(hWndList, LB_SETCURSEL, SelIndex, 0L); }
            }
             DispatchMessage(&msg); 
            
        }
    }

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,422 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,527 questions
{count} votes