Results 1 to 4 of 4
  1. #1
    kissofdiss's Avatar
    Join Date
    Mar 2011
    Gender
    male
    Posts
    101
    Reputation
    10
    Thanks
    413
    My Mood
    Cheerful

    Smile 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..
    Last edited by kissofdiss; 01-25-2014 at 02:06 PM.


    MY COMPUTER SPECS

    CPU - i5-6600k 3.5 GHz Quad-Core

    RAM - 32 GB G.Skill Ripjaws V 2133 mhz RAM

    Motherboard - MSI Z170A SLI PLUS ATX

    Hard Drives - WD 1 TB HDD, Corsair LS Force 120 GB SSD

    Video Card - Asus STRIX GTX 970 4 GB

    Operating System - Windows 10 Pro 64-bit


    Case - Corsair Obsidian 750D

  2. #2
    ♪~ ᕕ(ᐛ)ᕗ's Avatar
    Join Date
    Jun 2010
    Gender
    male
    Location
    Uterus
    Posts
    9,119
    Reputation
    1096
    Thanks
    1,970
    My Mood
    Doh
    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.

  3. #3
    abuckau907's Avatar
    Join Date
    Dec 2012
    Gender
    male
    Location
    other side of the wire
    Posts
    1,342
    Reputation
    162
    Thanks
    239
    My Mood
    Cold
    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 : )
    Last edited by abuckau907; 01-25-2014 at 09:14 PM.
    'Some things that can be counted, don't matter. And some things that matter, can't be counted' - A.E.
    --
     

    My posts have some inaccuracies/are wrong/wrong keyword(s) used.
    They're (maybe) pretty close, and I hope they helped you, not created confusion. Take with grain of salt.

    -if you give rep, please leave a comment, else it means less.

  4. #4
    Scim4niac's Avatar
    Join Date
    Jan 2014
    Gender
    male
    Location
    Brazil
    Posts
    5
    Reputation
    10
    Thanks
    0
    My Mood
    Busy
    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;
    }

Similar Threads

  1. [Solved] Keyboard
    By Xoyse in forum MapleStory Help
    Replies: 17
    Last Post: 08-30-2011, 02:23 PM
  2. [Help Request] JitBit won't detect keyboard inputs
    By falmay in forum Vindictus Help
    Replies: 19
    Last Post: 05-27-2011, 05:21 PM
  3. Replies: 20
    Last Post: 03-10-2009, 12:15 AM
  4. Working Hacks User helps User
    By oaschkropfn in forum Combat Arms Europe Hacks
    Replies: 6
    Last Post: 03-05-2009, 07:53 AM
  5. (help) my keyboard
    By takahuja in forum Suggestions, Requests & General Help
    Replies: 4
    Last Post: 07-02-2007, 09:31 AM