2

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.

Domenic
  • 110,262
  • 41
  • 219
  • 271
Briz
  • 548
  • 1
  • 9
  • 21

3 Answers3

4

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.

taylonr
  • 10,732
  • 5
  • 37
  • 66
  • This would be a good way to do it, but with the program I'm working on, it is very complicated to try to make a method do the same function as that little line. – Briz May 05 '11 at 15:45
  • It's the way our program is built. My boss actually wrote this code himself. – Briz May 05 '11 at 15:51
  • Understood... it's been a while since I've done C++, just thought I'd throw out a way to do it in a more manageable way. – taylonr May 05 '11 at 15:52
  • I appreciate it, but it's not what I'm asking. :) Thank you though. – Briz May 05 '11 at 15:53
  • I think what OP is saying is that he has no control on the receiver of the ESC key. He can only generate the ESC key and hope for the best. – Agnel Kurian May 05 '11 at 16:45
  • I get what the OP is asking for... I didn't realize that when the question was asked. I guess I could delete my answer, what is the stack etiquette in this situation? – taylonr May 05 '11 at 18:07
  • @taylonr - AFAIK it's fine to have an answer that doesn't help the author of the question. The question is here for all time (sort of) to help other people who have related problems, so maybe the answer will help someone else. –  May 23 '11 at 01:33
2

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

Summary: You can't reliably.

Community
  • 1
  • 1
RedX
  • 14,749
  • 1
  • 53
  • 76
-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;
    }
}
forsvarir
  • 10,749
  • 6
  • 46
  • 77
  • 1
    What's the purpose of the nested `if (getch()==27);`? Is swallows a key (no matter what it is), then always jumps to `loop`. The code also doesn't answer the question. The OP is trying to simulate an escape key, not keep looping until the user presses it. – forsvarir May 23 '11 at 08:40