Cant get Virtual Keys to work with a switch statement

Im simply trying to have it so if a user presses for example F1 then the program does whatever i have set in the switch statement. I have very little experience when it comes to working with windows so far and there are pretty much no tutorials that tell you how to do something this simple (yes i actually looked).

(This is just an example so if anything is clearly wrong dont be surprised)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <cstdlib>
#include <windows.h>
#include <string>


int main(){

    string a;
    
    switch(a){
        case:
            VK_F1:
                cout << "Whatever" << endl;
                system("pause");
                break;
    }
}


I pretty much just need help making this work.
Last edited on
It's just a matter of knowing the right term to search for. There are tons of tutorials and there are a few ways to do this. One of them is with GetAsynKeyState(VK_F1);

Another way, in windows, is to process the WM_KEYDOWN message.

Note that 'VK_F1' by itself is just a number. It only has meaning when it is used as a flag for GetAsynKeyState() or similar function.

Search 'how to capture keyboard event c++ tutorial'
Last edited on
Ya i saw the GetAsynKeyState before but wasn't sure if it was what i needed. Thanks though ill look into that some more
Try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <windows.h>
#include <stdio.h>

int main()
{
    while(1)
    {
         if(GetAsynKeyState(VK_F1))
         {
          printf("Helllllo!!!! =D");
          }
     }
     return 0;
}
Topic archived. No new replies allowed.