Hello again.
I was wondering ... how would I set a function to a certain key in a basic console app ? Quite simple, I'm sure, but I can't seem to make it work. I am using Dev-Cpp and Windows XP, just in case that matters.
Thanks in advance.
Hello again.
I was wondering ... how would I set a function to a certain key in a basic console app ? Quite simple, I'm sure, but I can't seem to make it work. I am using Dev-Cpp and Windows XP, just in case that matters.
Thanks in advance.
You can't set it to any value -- function keys already have ascii values. This code snippet was compiled with VC++ 6.0, uses non-standard conio.h, which may or may not be implemented by other compilers.
your console program will have to check the ascii value of the keypress, similar to what it does in the code below.
Code:#include <iostream> #include <conio.h> using namespace std; #define ESC 27 #define F1 -59 #define F2 -60 #define F3 -61 #define F4 -62 #define F5 -63 #define F6 -64 #define F7 -65 #define F8 -66 #define F9 -67 int main() { int key = 0; while(key != ESC) { key = getch(); if(key == 0) key = -getch(); switch(key) { case F1: cout << "F1 hit" << endl; break; case F2: cout << "F2 hit" << endl; break; case F3: cout << "F3 hit" << endl; break; case F4: cout << "F4 hit" << endl; break; case F5: cout << "F5 hit" << endl; break; case F6: cout << "F6 hit" << endl; break; case F7: cout << "F7 hit" << endl; break; case F8: cout << "F8 hit" << endl; break; case F9: cout << "F9 hit" << endl; break; default: cout << key << endl; break; } } return 0; }
Right ... so what are the "#defines" s doing then ?
Cheers for the reply.
Well, if you don't already know the answer to that, then you need to start at the beginning -- buy an Introduction to C programming book, such as one of the Teach Yourself series.Originally Posted by Necrofear
No, I mean that surely the program would automatically know the ASCII values of the keypresses. You wouldn't need to define them.
whatever makes you think that? There is no standard definition of them. They are defined in windows.h (winuser.h) if you are using MS-Windows operating system, such as VK_F1, and I suppose curses.h has then defined too, but I don't know what they are.
Oh, okay cheers.
So how would you define numbers and letters then ? Since I can't just put :
How would I define them ?Code:#define 1 49 #define 2 50 #define 3 51 #define 4 52 #define 5 53
you don't have to define them at all -- just use 'A' if you want to refer to an A. Function keys do not have ascii values like normal alphabetical characters.
Ah !
Success ! Thanks a million !
what about the ctrl, shift and alt keys?
using standard C functions you can't capture those keys themselves. All you can do is capture the Ctrl+<Some other key here>, such as Ctrl+A will return 1.Originally Posted by h_howee
I think you can using some Win32 api functions, but not certain.