Search code examples
c++winapicrashinfinite

Infinite while loop crashes WinAPI


I'm still kind of new to C++ and WinAPI and I've come across an issue. I can't make use of an infinite while loop (while(true) in this case) in WinAPI but I can do exactly what I want to do in a console application without the crashing.

I'm trying to detect a key-press so I believe that I need this loop unless someone can provide other means of doing this.

while (true) {
  if (GetAsyncKeyState(MIDDLEMOUSEBUTTON)) {
    //my code here
  }
}

Not responding

Any help or advice with this is appreciated, thanks.


Solution

  • GUI apps are event driven. Your loop is preventing a message loop from processing new messages. That is why your app is unresponsive. You need a message loop, and you need your message handler to process WM_KEYDOWN, WM_KEYUP, and/or WM_CHAR messages to handle key presses, WM_(L|M|R)BUTTONDOWN and/or WM_(L|M|R)BUTTONUP messages to handle mouse clicks, etc.