I want to write a function in which when you tap "esc" the the window should close, the thing is that the function should be written in c. I'am new to the c programming any help pls.
-
C doesn't have windows, or keys. – Kerrek SB Jun 06 '13 at 10:06
-
3It would help posters if you could give some more background on what you're doing - what OS, framework etc. Also, people here appreciate it if the person asking the question has had at least a minimal go at solving the problem. Do some research on your own, try something and if you're still stuck paste the code here. People will be happy to help. – Nobilis Jun 06 '13 at 10:09
-
Please post your OS - how this is done in Windows and UNIX/Linux is vastly different. And as @KerrekSB said there really are no "keys" in C. – jim mcnamara Jun 06 '13 at 10:10
3 Answers
Well, for Windows:
GetActiveWindow or GetFocus to retrieve the window that is focused on.
GetASyncKeyState to capture the escape button.
SendMessage to close the respective window.
If you want something fancier than GetASyncKeyState(which is not reliable and expensive to the processor in an infinite loop) you can do something relatively difficult at first sight: global keyboard hooks in c
void myfunc(){
char ch;
do{
//do what you want here
ch=getchar();
}while(ch!='27'); //27 is the ascii code for esc
}

- 1,545
- 12
- 20
There is no standard way to react immediately to input in C - the ESC key is no different from the letter 'A' or 'x' in this respect - the input is not given to the program until the user hits Enter. Sometimes, a further complication is that ESC is used for special purposes, for example a Windows command-line application uses ESC for "erase whatever I've typed so far", which makes it impossible to read even with "ESC + Enter".
So, you need to use system specific functions that can read "raw" data from the keyboard, rather than the usual "cooked" data that you get using the standard I/O functions (e.g. getchar()
).
There are OS-independent options, such as ncurses
that supply this for console applications.
In windows, you may be able to use getch()
after including conio.h
, but this is a non-standard library, and it only works in a console application.
In a windowed environment it's a case of responding to a keyboard event, and "if key == ESCAPE close_window" - exactly how you do that depends on the windowing system and framework used.

- 126,704
- 14
- 140
- 227
-
I am the second downvote. My explanation is that your answer is terrible. ESC is ASCII value 27. When you use ```getchar()```, that will be value 27 decimal you are looking for. It hardly gets any simpler than that. The issue is that you are making stuff seem harder than it really is, a somewhat stereotypical outcome. You seem to think that Esc is a special keyboard key, not an ASCII character code. Esc is no more special than M. – Heath Hunnicutt Jun 06 '13 at 16:38
-
@HeathHunnicutt: Do you actually think the original poster means "ESC + enter", or "hit ESC alone". As far as I can tell, the latter is not possible with standard C. The former is definetely possible - assuming ESC isn't "used up" by the keyboard input processing itself - which tends to be the case in Windows, as it means "Erase the entire line" - and thus, pressing ESC in Windows command line code doesn't work. But I will rephrase the answer to more precisely describe the scenario that is "not possible" for your benefit. – Mats Petersson Jun 06 '13 at 16:48
-
OP doesn't even state which OS. If you want to assume line-buffering, that's you. If you do assume line-buffering, you may as well mention stty(), the obvious solution to your concept of "not possible." As for the behavior of Windows, you seem to be invoking FUD. – Heath Hunnicutt Jun 06 '13 at 17:22
-
Name ONE OS where `stdin` is not line-buffered (without changing the default settings for the input console/terminal/whatever). I certainly haven't worked with one. I've worked with OS's that doesn't have `stdin`, but that's neither standard, nor what I think we are talking about. Sure, we can use `stty` or `tcsetattr` to change the default setting - that's NOT standard C (it may be standard POSIX, but not standard C). – Mats Petersson Jun 06 '13 at 22:37