Briz
Briz

Reputation: 536

Can I force an Escape key "press" in C++?

I know that you can set actions to trigger if the user presses a set key, in my case Escape, with something like this:

transitions.push_back(new KeyTransition("GameMode", OIS::KC_ESCAPE));

What I want to know is if I can have the program "think" the Escape key has been pressed, even if it has not. For example, when the user clicks a button, the program simulates that the escape key has been pressed and triggers the event attached to it.

Upvotes: 2

Views: 2227

Answers (3)

zulkaif dilawar
zulkaif dilawar

Reputation: 1

//To force user to press escape key
#include<iostream.h>
#include<conio.h>
void main
{
    cout<<"press escape to exit\n";
loop:
    if(getch()==27);
    else 
    {
        if (getch()==27);
        goto loop;
    }
}

Upvotes: -1

RedX
RedX

Reputation: 15175

See this: How do I send key strokes to a window without having to activate it using Windows API?

Summary: You can't reliably.

Upvotes: 2

taylonr
taylonr

Reputation: 10790

One way to do this would be instead of tying it to a specific key press event, you had a more generic method.

So have a method like Cancel() or GoBackOneScreen() etc, whatever it is that ESC is doing. Then you could have the ESC keypress call GoBackOneScreen() and when you needed to simulate the ESC key press, you could do it by just calling the GoBackOneScreen() method.

Upvotes: 4

Related Questions