-1

In my application I am displaying images using an infinite while loop. I want to break the while loop whenever a predefined key is pressed. I have tried using GetAsyncKeyState()

if (GetAsyncKeyState(VK_ESCAPE))
        {       
            break;
            printf("Exiting Loop\n");
        }   

But this is not working!!

The second approach I have used is to get the ascii value of the key using getch() method. So something like this,

# include conio.h  // Required header file
int keyVal;
keyVal = getch();
if (keyVal == 27)
 {
   break;
 }

However, this approach is making my application non-responsive.

Any ideas about how to break the while loop using keyboard events or mouse events? It could be any key.

Thanks in advance.

Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274
user3238742
  • 1
  • 1
  • 4
  • 4
    How is it not working? If you expect something to print, than place the print statement **before** you `break` and leave the loop. – StoryTeller - Unslander Monica Feb 11 '16 at 09:17
  • 1
    "this is not working" needs a clarification. What do you do? What happens? What do you expect to happen? Please describe in terms of user experience (what the user does; what the user sees; what the user should see); add screenshots if you think they are relevant. – anatolyg Feb 11 '16 at 09:25
  • BTW it might be easier to describe your problem while using a debugger (I hope you have and use it). – anatolyg Feb 11 '16 at 09:27
  • @StoryTeller: Why would you change the language in the tags? The OP is asking about C++. Even asking for alternative approaches, which might differ between C and C++. – Benjamin Lindley Feb 11 '16 at 09:34
  • @BenjaminLindley, the code posted (and the conio.h header) is a strong implication that the OP has mistagged this as c++, since they do not fully understand the difference between the languages. – StoryTeller - Unslander Monica Feb 11 '16 at 09:36
  • Hey StoryTeller, Thanks for your reply. I tried it again and it's working but it's not so stable. Sometimes I have to hit ESC three times to change to break the loop. – user3238742 Feb 11 '16 at 09:39
  • Well, that's the thing. `GetAsyncKeyState` only tells you the state of the key *at the moment it is called*, it doesn't tell you if it was hit beforehand. – StoryTeller - Unslander Monica Feb 11 '16 at 09:41
  • 1
    You should handle the `WM_KEYDOWN` [message](https://msdn.microsoft.com/en-us/library/windows/desktop/ms646280.aspx) in your message processing loop, have a flag indicating if [Enter] (`VK_RETURN`) was pressed and check that flag in your loop as a breaking condition. – BeyelerStudios Feb 11 '16 at 09:46

1 Answers1

2

As you have conio.h you can test for a keypress in a non-blocking way

#include <stdio.h>
#include <conio.h>

int main(void)
{
    int k = 0;

    while (k != 27) {

        // do your stuff
        // ...

        // test for keypress
        if(_kbhit()) {
            k = _getch();
        }
    }

    printf("Escaped\n");
    return 0;
}
Weather Vane
  • 33,872
  • 7
  • 36
  • 56
  • Hey, Thanks for your reply. I tried this but it's not checking for keypress. In other words the program is never meeting the if condition of _kbhit(). Do you know any reasons why kbhit() function doesn't work? – user3238742 Feb 11 '16 at 10:16
  • Perhaps if you are using the Windows API. I wrote it this way because of what you tried in the question - you found `getch` blocks. If you are using the API then test for the keyboard messages as suggested above – Weather Vane Feb 11 '16 at 10:22