• Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.
    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Solved - C++ How do I detect if a key is pressed?

Status
This thread has been solved! Go to solution…

Master Yoda

Administrator
Administrator
Verified
Jan 2, 2018
1,720
432
95
Canada
codeforum.org
Hello.

I'm trying to write a program where when you press a certain key within console it will then display a message.

C++:
if(GetAsyncKeyState(VK_W)) {
        std::cout << "You pressed W" << std::endl;
    }
I believe the issue lies within VK_W, I just have a feeling this isn't correct. Can someone help me find a solution to this?
 

Mathematical

Coder
Mar 30, 2020
145
20
18
Hey, @Master Yoda.

I'm not a C++ programmer, as you know, but doing a quick search online, brings me to the MSDN website. More specifically, this page: https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getasynckeystate

There appears to be no constant variable declared and initialized as VK_W. So, there's no doubt that the issue lies within this new constant that you've just made. This forum post on cplusplus.com, provides examples from other users: https://www.cplusplus.com/forum/windows/6632/

Also, since this function appears to be only available on MS Windows, make sure you have included the windows.h header-file in your code.

If you get any other errors, read over the documentation and forum-posts again, do a little of searching for your issue, and theb come back here with your results.
 

Master Yoda

Administrator
Administrator
Verified
Jan 2, 2018
1,720
432
95
Canada
codeforum.org
Hey, @Master Yoda.

I'm not a C++ programmer, as you know, but doing a quick search online, brings me to the MSDN website. More specifically, this page: https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getasynckeystate

There appears to be no constant variable declared and initialized as VK_W. So, there's no doubt that the issue lies within this new constant that you've just made. This forum post on cplusplus.com, provides examples from other users: https://www.cplusplus.com/forum/windows/6632/

Also, since this function appears to be only available on MS Windows, make sure you have included the windows.h header-file in your code.

If you get any other errors, read over the documentation and forum-posts again, do a little of searching for your issue, and theb come back here with your results.
Unfortunately doesn't seem like that helped to solve my issue :(
 

Mathematical

Coder
Mar 30, 2020
145
20
18
Unfortunately doesn't seem like that helped to solve my issue :(
Huh. Did you read the documentation properly and were you able to understand it? Sometimes you may need to give everything a re-read just to properly understand it(If that's the issue).

I would ask for the code to perhaps compile it for myself, but I'm on a GNU/Linux system, not an MS Windows system, so I can't really do much. Although, perhaps posting the rest of the code might expand our search to track down the issue. There might be something that you've did in a part of the code that we can't see right now, that's causing the issue(That of course, is just an assumption). If you stand firm on your ground that the snippet that you shared with us is where the issue is happening, then we'll stick to that for now anyway.

As for your code, looking back on the documentation again, there is no VK_W that's part of the <windows.h> header. So, as I said, I believe that you might've declared a constant of your own(Remember that constants are typically written all in upper-case). This goes back to my earlier statement, that the issue may lie within some other part of the code that we can't see right now. Remember that in the MS docs, these are the only VK_... that are in this function(GetAsyncKeyState()):
  • VK_LSHIFT
  • VK_RSHIFT
  • VK_LCONTROL
  • VK_RCONTROL
  • VK_LMENU
  • VK_RMENU
 

Master Yoda

Administrator
Administrator
Verified
Jan 2, 2018
1,720
432
95
Canada
codeforum.org
Huh. Did you read the documentation properly and were you able to understand it?
Yes, @Mathematical I have read the documentation properly and of course I was able to understand it.

However, I was not able to solve the problem using your way.

Here is my solution:
C++:
#include <iostream>
#include <conio.h>

int main()
{
    char ch; //or 'int ch;' (it doesn't really matter)

    //the program pauses here until a key is pressed
    ch = _getch();

    if(ch == 'w')
        std::cout << "You pressed a!" << std::endl;
    else
        std::cout << "You did not press a!" << std::endl;

    return 0;
}
 
  • Like
Reactions: Mathematical

Krusty the Senile

Active Coder
Jun 26, 2019
108
29
28
I thought I would need a whole bunch of win32 boilerplate code, but it seems not.

Relatively small example:

C++:
#include <iostream>
#include <Windows.h>

int main()
{
    long counter = 0;
    SHORT lastState = 0;
    while (1)
    {
        SHORT state = GetAsyncKeyState(VK_SPACE);
        if (lastState != state)
        {
            std::cout << std::dec << counter << " key state space changed " << std::hex << state << std::endl;
            lastState = state;
        }
        if (GetAsyncKeyState(VK_RETURN))
            break;
        counter++;
        Sleep(10);
    }
    std::cout << "Finito" << std::endl;
}
I'm guessing in your example, you're expecting the GetAsyncKeyState() function to block until there was a keypress event, but I think the intention of this function is to check and return straight away. Based on the behaviour, it was working kinda weird, but that may have been because it's a console application. That function may work differently within a win32 application with the win32 message loop, or different key handling functions should be used.

I originally tried to use VK_A but the definition wasn't found (or I didn't look hard enough).
 
Status
This thread has been solved! Go to solution…