"Function Keys"

This is a discussion on "Function Keys" within the C++ Programming forums, part of the General Programming Boards category; Hello again. I was wondering ... how would I set a function to a certain key in a basic console ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    183

    "Function Keys"

    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.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    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;
    }

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    183
    Right ... so what are the "#defines" s doing then ?

    Cheers for the reply.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by Necrofear
    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.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    183
    No, I mean that surely the program would automatically know the ASCII values of the keypresses. You wouldn't need to define them.

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    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.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    183

    Thanks

    Oh, okay cheers.

    So how would you define numbers and letters then ? Since I can't just put :

    Code:
    #define 1        49
    #define 2        50
    #define 3        51
    #define 4        52
    #define 5        53
    How would I define them ?

  8. #8
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    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.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    183
    Ah !

    Success ! Thanks a million !

  10. #10
    Registered User
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    267
    what about the ctrl, shift and alt keys?

  11. #11
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by h_howee
    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.

    I think you can using some Win32 api functions, but not certain.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Conceptual ambiguity "function prototype"
    By password636 in forum C Programming
    Replies: 4
    Last Post: 04-02-2009, 12:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21