0

I want to detect function keys such as f1 to save f2 to refresh in console using c++ so I can add further functionality.

Ali
  • 41
  • 1
  • 3
  • 2
    [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask) – Beta Carotin Jul 12 '15 at 12:14
  • 1
    This is very dependent on operating system, but often special keys like the function keys are sent using multiple characters. Also, it's not certain that the console window catches some key-presses, other programs may intercept them before the console program has a chance to receive them (think, for example, on the multimedia keys on some keyboards). – Some programmer dude Jul 12 '15 at 12:17
  • 2
    The C++ language itself does not know anything about function keys. C++ programs may (and do) run on computers without keyboards or screens. Functionality like this is therefore supplied by the operating system, which typically provides some special (non-standard) C headers you can use with C++. – Christian Hackl Jul 12 '15 at 12:22

4 Answers4

2

Check this example:

#include <conio.h>

using namespace std;

int main()
{
    cout << "Press any key! CTRL-D to end" << endl;
    while(1)
    {
        unsigned char x = _getch();
        if (0 == x)
        {
            printf("Function key!\n");
            x = _getch();
        }
        printf("key = %02x\n", x);
        if (x == 4) break;
    }
    return 0;
}

When you press a function key, you'll get a zero followed by another code. Use that code to determine which F-key you got.

Logicrat
  • 4,438
  • 16
  • 22
  • 3
    For the OP it should be noted that this only work on systems which have the non-standard `` header and the non-standard functions `_getch`, which is as far as I now only Windows. – Some programmer dude Jul 12 '15 at 12:52
0

If you are on windows console, you can use ReadConsoleInput() to detect and process keyboard events.

All keys are mapped as virtual keys. The F function keys are mapped as VK_F1 to VK_F12.

Below is an implementation of console keyboard handling.

#include <stdio.h>
#include <windows.h>
#include <iostream>
using namespace std;


void save();
void refresh();


int main(){
    DWORD        mode;          /* Preserved console mode */
    INPUT_RECORD event;         /* Input event */
    BOOL         QUIT = FALSE;  /* Program termination flag */

    /* Get the console input handle */
    HANDLE hstdin = GetStdHandle( STD_INPUT_HANDLE );

    /* Preserve the original console mode */
    GetConsoleMode( hstdin, &mode );

    /* Set to no line-buffering, no echo, no special-key-processing */
    SetConsoleMode( hstdin, 0 );


    while (!QUIT){           


        if (WaitForSingleObject( hstdin, 0 ) == WAIT_OBJECT_0){  /* if kbhit */

            DWORD count;  /* ignored */

            /* Get the input event */
            ReadConsoleInput( hstdin, &event, 1, &count );
            cout<<"Key Code   = "<<event.Event.KeyEvent.wVirtualKeyCode <<" \n";

        }

            /* Only respond to key release events */
            if ((event.EventType == KEY_EVENT)
            &&  !event.Event.KeyEvent.bKeyDown){        


                    switch (event.Event.KeyEvent.wVirtualKeyCode){

                        case VK_ESCAPE:
                           QUIT = TRUE;
                         break;


                        case VK_F1:
                                  save();
                         break;


                       case VK_F2:
                                  refresh();
                         break;


                       case VK_F5:

                         break;


                       case VK_F8:

                         break;


                    }//switch

                   event.Event.KeyEvent.wVirtualKeyCode=-1;             

        }
    }

 return 0; 
}



void save(){

}


void refresh(){

}

More key mappings : https://msdn.microsoft.com/en-us/library/windows/desktop/dd375731%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396

Software_Designer
  • 8,490
  • 3
  • 24
  • 28
0

F1-F12 work cross-platform with this solution.

On Windows, F11 toggles fullscreen mode in the command prompt and thus cannot be detected as key press.

ProjectPhysX
  • 4,535
  • 2
  • 14
  • 34
0

Try this :

#include <iostream>
#include <system.h>
int main(){
while(true){
Sleep(10);
if(GetKeyState('R') & 0x8000){
std::cout << "R Pressed";
}
}
return 0;
}
Harsh Uppal.
  • 26
  • 1
  • 4