Thread: GetAsyncKeyState.

  1. #1
    deletedforumuser
    Guest

    GetAsyncKeyState.

    GetAsyncKeyState is global, even if my application is minimized, it will check for key input. Is there any way of doing it for a single application?

  2. #2
    Registered User
    Join Date
    Jun 2008
    Posts
    266
    You could either check for input with GetAsyncKeyState only if your window is active, or you could just use Windows messages which does that for you. It all depends on what you're making.

  3. #3
    deletedforumuser
    Guest
    I'm making a game, i need a key input system for a console based application.

    There's a way of hooking async with one program...but how to?

  4. #4
    Registered User
    Join Date
    Jun 2008
    Posts
    266
    As far as I know, no. I would try my first idea. Just check if your window is active using IsWindowEnabled, and if it is then check for input with GetAsyncKeyState.

    Edit: This probably also belongs in Windows...

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You really do not need to mess around with this is you used a real input API, such as DirectInput. It was made for games.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Jun 2008
    Posts
    266
    Microsoft now recommends raw input over DirectInput. There probably are alternative, more portable game input libraries available though. Check into SDL, although I recommend SFML.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Moved to Windows Programming forum.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Code:
    if(GetAsyncKeyState(key) && GetFocus()==yourhwnd){
        handle the input
    }
    Last edited by maxorator; 11-09-2008 at 02:19 AM.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    137
    Since you will be using this in a game, you will go into your GetAsyncKeyState loop, so first you wanna check the GetFocus() == yourhwnd, and then go into your key waiting loop.
    ★ Inferno provides Programming Tutorials in a variety of languages. Join our Programming Forums. ★

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Just don't burn out the CPU. Check if the key was pressed down (or up) since the last call. It is a semi-buffered API, but it is better than nothing.
    Although better would be to use a real buffered API such as that DirectInput & probably other input API offers.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GetAsyncKeyState Problem
    By kevinawad in forum C++ Programming
    Replies: 1
    Last Post: 11-07-2008, 06:00 PM
  2. Problems with cin and GetAsyncKeyState
    By KgNe in forum C++ Programming
    Replies: 32
    Last Post: 08-21-2008, 10:00 AM
  3. Infinite Loop with GetAsyncKeyState
    By guitarist809 in forum Windows Programming
    Replies: 1
    Last Post: 04-18-2008, 12:09 PM
  4. Problems with GetAsyncKeyState
    By blurrymadness in forum C++ Programming
    Replies: 13
    Last Post: 04-21-2007, 06:13 PM