HelpπŸ™‚ How can I get keyboard inputs from user c++?

Posts 1–4 of 4 Β· Page 1 of 1
πŸ™‚ How can I get keyboard inputs from user c++?
Hello, I just got into C++ basics and I would like to know how can I get user keyboard inputs, for example if a user presses F1 something happens, This is what I got so far but it's not working

Code:
#include "stdafx.h";
#include <iostream>;
#include <windows.h>;
#include <sstream>;

main()
{
     string something = "OFF";
     int f1;
     cout << something << endl;
     cin >> f1;
     if(f1 == GetAsyncKeyState(VK_F1))
            something = "ON";

     system("PAUSE");
     return 0;
}
I'm getting nothing though, :/ it's really frustrating cus Idk what to do Q.Q! I opened every single link on google, nothing..
You're reading the user input into the console's stream (idk I'm using a .NET way of talking lol). GetAsyncKeyState has to be put into a loop to keep checking for user's input.
Code:
while(true)
{
     if(GetAsyncKeyState(VK_F1) == TRUE)
          // Do some code.
}
Since it's an infinite loop you should execute it in a different thread. Check CreateThread on MSDN and also take a look on timers.
There are two type of keyboard input you're dealing with...


cin >>

will automatically read (and show on the screen) when you press keys. For example when typing your name, or a command, etc. If your program is a console window, and you want the user to type something in, you should be using cin (or something similar). This is the normal way a user would "give keyboard input" to the program.

and

GetAsyncKeyState() can be used at any time. So if you're making a mod for a FPS game..you want the game open and your console to be minimized and out of the way --> in this case you'd use GetAsyncKeyState() (because the user won't have the console open and ready to type to it).


normal user input = cin (or getline - if the input has spaces cin won't work..read about it)

"hot key/shortcut keys" = getasynckeystate()

edit: so if you're trying to make a menu system in the console (ie. the user picks 1-8, etc), use cin. If you want hotkeys (after re-reading post, I think you do), use a loop and getasynckeystate. -multithreading and loops is a little much to explain to a new person. Do a little reading on it, look at a few examples, and come back with an informed question : )
It's better to create a thread specifically for capturing key events (so it doesn't freeze the flow of your program).

for exemple:

Code:
#include <cstdio> // faster than streams
#include <windows.h>

DWORD WINAPI captureKeyEvs(LPVOID lpArg) {
    int evCount = 0;
    while(!false) {
        if(GetAsyncKeyState(VK_LBUTTON)) printf("Shoot %d\n", ++evCount);
    }
}

int _tmain (void) {
    DWORD dwThreadID;
    HANDLE hThread;

    hThread = CreateThread(NULL, 0, captureKeyEvs, NULL, 0, &dwThreadID);

    for(int i = 0; !(!true); ++i) {
        printf("Tick %d\n", i);
        Sleep(1000);
    }

    return 0;
}
Posts 1–4 of 4 Β· Page 1 of 1

Post a Reply

Similar Threads

Talk with us