1

I'm trying to just stop my program whenever ESC Key is pressed.

For example i have code like this :

char key;
char msg[20];
printf("\n\n\t\t     press escape to quit\n\n");
do {
    key = _getch();

    if (key == ESC) {
        printf("Key: ESCAPE");
        putchar('\n');

    }
    else {
        printf("Key: %c", key);
        putchar('\n');
    }
    Sleep(5000);


} while (key != ESC);

return 0;

And now when i need to exit my program instantly whenever i press ESC is it possible to do? My program gonna exit after 5 sec.. after

Sleep(5000)

All i want to do is exit program instantly when ESC is pressed.

How can i do that?

Ðаn
  • 10,934
  • 11
  • 59
  • 95
Adam Zbudniewek
  • 107
  • 2
  • 12
  • Did you try to put a break in the branch when the ESC is pressed? – dmi Feb 23 '17 at 09:01
  • What do you mean put break in the branch can you give example? Is is another way to Stop whole program and ingore this sleep function? I think i need to create a hotkey . In autoit its simple really just createing a hotkey and when i press ESC - main program stop main loop and just do instruction in my hotkey . How can i do that in c++ , any ideas? – Adam Zbudniewek Feb 23 '17 at 09:33
  • Adding the Sleep() call was a mistake. You think you needed so you could see the output of your printf() statement. You didn't, press Ctrl+F5 to see that. You can make it perfect by using GetConsoleProcessList() to detect that you need a "Press any key to continue" style interaction because the console window is going to disappear when your program ends. – Hans Passant Feb 23 '17 at 09:45
  • @UP you're right man but your answear didnt help me. Is it possible to create hotkey function outside of main loop , that will be waiting all time for press key and just do a instruction ? – Adam Zbudniewek Feb 23 '17 at 09:59
  • 1
    if (key == ESC) { printf("Key: ESCAPE"); putchar('\n'); break; <--------- to terminate your while loop without Sleep invocation. } – dmi Feb 23 '17 at 10:10
  • @UP yeah it works, but when i change position of Sleep() and i get it before if (key == ESC) . Is it possible to again terminate without waiting this 5 seconds? – Adam Zbudniewek Feb 23 '17 at 10:58

3 Answers3

1

The simplest way is to move the sleep call into the else part so it is executed only if a key <> 'ESC' is pressed.

char key;
char msg[20];
printf("\n\n\t\t     press escape to quit\n\n");
do {
    key = _getch();

    if (key == ESC) {
        printf("Key: ESCAPE");
        putchar('\n');
    }
    else {
        printf("Key: %c", key);
        putchar('\n');
        Sleep(5000);
    }

} while (key != ESC);

return 0;
D. Mika
  • 2,577
  • 1
  • 13
  • 29
  • YES you are true , but this is only a example my code is more more bigger with more functions ... All i need is create a hotkey whenever i press ESC program exit. In main loop i have 'Sleep()' functions and more and now i need to check whenever press key just end whole program – Adam Zbudniewek Feb 23 '17 at 09:27
  • @Adam Zbudniewek: So you want to exit your program when ESC is pressed even when it's doing some heavy work and is not able to handle keyboard input, right? – D. Mika Feb 23 '17 at 12:10
1

Well, you can compare it with it's ASCII code. ASCII code for ESC key is 27.

 char key;
char msg[20];
printf("\n\n\t\t     press escape to quit\n\n");
do {
  key = _getch();

  if (key == 27) {
      printf("Key: ESCAPE");
      putchar('\n');
  }
  else {
      printf("Key: %c", key);
      putchar('\n');
      Sleep(5000);
  }

} while (key != 27);
Amir Ali
  • 11
  • 7
0

Why you check key two times in loop ? You can do this with only one check.

What about this ?

int key;
char msg[20];
printf("\n\n\t\t     press escape to quit\n\n");

while(true) {
    key = _getch();
    if(key == ESC) {
        printf("Key: ESCAPE");
        putchar('\n');
        break;
    }

    printf("Key: %c", key);
    putchar('\n');
    Sleep(5000);
}
Gor
  • 2,808
  • 6
  • 25
  • 46
  • Yep , its good, but is it possible to make 2 functions run exacly in the same time? 1 loop still running .... and 2 loop can run in the same time is that possible ? – Adam Zbudniewek Feb 23 '17 at 10:34
  • @AdamZbudniewek Yes it is possible. To run two loops at the same time, you will need to put them in different threads. Maybe start here to understand threading. Also check this question http://stackoverflow.com/questions/26425836/any-way-to-run-2-loops-at-the-same-time – Gor Feb 23 '17 at 11:47