Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. GetAsyncKeyState() and GetKeyState() [Solved]

GetAsyncKeyState() and GetKeyState() [Solved]

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
12 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Rozis

    Can anyone give me the precise difference between GetAsyncKeyState(vk_shift) and GetKeyState(vk_shift) ? I can and did read the explainment of MS, but i guess i still don't get it. In my apps i use GetAsyncKeyState(vk_shift)!=0 to find out if the shift-key is pressed. Should i use GetKeyState() instead and if yes, why? And could you give me an example of how to check the shift key with that one. If no, where is GetKeyState() then good for? Can you give an example of its use? Thanks

    CPalliniC Offline
    CPalliniC Offline
    CPallini
    wrote on last edited by
    #2

    I think the hint is in the name: you should call GetKeyState after a Keyboard message has been sent to your thread (MSDN [^]: "An application calls GetKeyState in response to a keyboard-input message."). On the other hand, you may call GetASyncKeyState whenever you need it. :)

    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
    [My articles]

    In testa che avete, signor di Ceprano?

    R 1 Reply Last reply
    0
    • CPalliniC CPallini

      I think the hint is in the name: you should call GetKeyState after a Keyboard message has been sent to your thread (MSDN [^]: "An application calls GetKeyState in response to a keyboard-input message."). On the other hand, you may call GetASyncKeyState whenever you need it. :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
      [My articles]

      R Offline
      R Offline
      Rozis
      wrote on last edited by
      #3

      Ok, get it but:

      CPallini wrote:

      On the other hand, you may call GetASyncKeyState whenever you need it.

      What does it give back then: The current state or the state it had after the last keyboard message or ...?

      According to MSDN:

      The key status returned from this function (GetKeyState) changes as a thread reads key messages from its message queue. The status does not reflect the interrupt-level state associated with the hardware. Use the GetAsyncKeyState function to retrieve that information.

      "The status does not reflect the interrupt-level state associated with the hardware." What's this? (i do know the concept of interrupts) Is it a strange way of telling me that GetKeyState() could give me a key-status that is actually outdated already?

      CPalliniC 1 Reply Last reply
      0
      • R Rozis

        Ok, get it but:

        CPallini wrote:

        On the other hand, you may call GetASyncKeyState whenever you need it.

        What does it give back then: The current state or the state it had after the last keyboard message or ...?

        According to MSDN:

        The key status returned from this function (GetKeyState) changes as a thread reads key messages from its message queue. The status does not reflect the interrupt-level state associated with the hardware. Use the GetAsyncKeyState function to retrieve that information.

        "The status does not reflect the interrupt-level state associated with the hardware." What's this? (i do know the concept of interrupts) Is it a strange way of telling me that GetKeyState() could give me a key-status that is actually outdated already?

        CPalliniC Offline
        CPalliniC Offline
        CPallini
        wrote on last edited by
        #4

        GetAsyncKeyState gives the current state.

        Rozis wrote:

        s it a strange way of telling me that GetKeyState() could give me a key-status that is actually outdated already?

        Yes, it maybe outdated. :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        In testa che avete, signor di Ceprano?

        R 1 Reply Last reply
        0
        • CPalliniC CPallini

          GetAsyncKeyState gives the current state.

          Rozis wrote:

          s it a strange way of telling me that GetKeyState() could give me a key-status that is actually outdated already?

          Yes, it maybe outdated. :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          R Offline
          R Offline
          Rozis
          wrote on last edited by
          #5

          CPallini wrote:

          GetAsyncKeyState gives the current state.

          OK. What I learned so far (correct me please if i'm wrong): GetKeyState() returns if a key is pressed based on the current status of the messagequeue. The status of the messagequeue is formed by the keyboard messages sent and processed to this queue. GetAsyncKeyState(), on the other hand, returns if a key is pressed based on the current status of the keyboard hardware. In most cases both functions have the same outcome. There are some differences: 1) Synthesizing a keypress would work when GetKeyState() was used but not when GetAsyncKeyState() was used. - Am i right? And what about keyboard hooks? 2) As Adam Roderick J 09 points out GetKeyState is useful for VK_NUMLOCK or VK_CAPITAL, which depends on toggle values. GetKeyState buffers these values. GetAsyncKeyState gives you only if these keys are currently pressed or not, not if they are on or off. Avi Berger gave this link: http://blogs.msdn.com/oldnewthing/archive/2004/11/30/272262.aspx[^]

          modified on Sunday, March 21, 2010 5:01 PM

          1 Reply Last reply
          0
          • R Rozis

            Can anyone give me the precise difference between GetAsyncKeyState(vk_shift) and GetKeyState(vk_shift) ? I can and did read the explainment of MS, but i guess i still don't get it. In my apps i use GetAsyncKeyState(vk_shift)!=0 to find out if the shift-key is pressed. Should i use GetKeyState() instead and if yes, why? And could you give me an example of how to check the shift key with that one. If no, where is GetKeyState() then good for? Can you give an example of its use? Thanks

            A Offline
            A Offline
            Adam Roderick J
            wrote on last edited by
            #6

            Just go throught my new article, that will help you clear with all the doubts regarding the ose APIs Mouse and KeyBoard Hooking utility with VC++.

            Величие не Бога может быть недооценена.

            R 1 Reply Last reply
            0
            • A Adam Roderick J

              Just go throught my new article, that will help you clear with all the doubts regarding the ose APIs Mouse and KeyBoard Hooking utility with VC++.

              Величие не Бога может быть недооценена.

              R Offline
              R Offline
              Rozis
              wrote on last edited by
              #7

              Then it must be a peace of cake for you to point me out the precise difference between GetAsyncKeyState() and GetKeyState() ?

              A 1 Reply Last reply
              0
              • R Rozis

                Can anyone give me the precise difference between GetAsyncKeyState(vk_shift) and GetKeyState(vk_shift) ? I can and did read the explainment of MS, but i guess i still don't get it. In my apps i use GetAsyncKeyState(vk_shift)!=0 to find out if the shift-key is pressed. Should i use GetKeyState() instead and if yes, why? And could you give me an example of how to check the shift key with that one. If no, where is GetKeyState() then good for? Can you give an example of its use? Thanks

                A Offline
                A Offline
                Avi Berger
                wrote on last edited by
                #8

                Rozis wrote:

                In my apps i use GetAsyncKeyState(vk_shift)!=0 to find out if the shift-key is pressed.

                In most cases you want to use GetKeyState() and not GetAsyncKeyState(). If the shift-key was pressed when? Are you processing a particular keystroke? (Such as in an OnKeyDown handler?) GetKeyState() tells you the state at the time of the event that you are processing. GetAsyncKeyState() tells you the state at the instant that you call it. This is not coordinated with where your program is in processing its input stream. For a longer explanation, see here[^].

                Please do not read this signature.

                R 1 Reply Last reply
                0
                • R Rozis

                  Then it must be a peace of cake for you to point me out the precise difference between GetAsyncKeyState() and GetKeyState() ?

                  A Offline
                  A Offline
                  Adam Roderick J
                  wrote on last edited by
                  #9

                  GetAsyncKeyState function determines whether a key is up or down at the time the function is called, and whether the key was pressed after a previous call to GetAsyncKeyState. GetKeyState function retrieves the status of the specified virtual key. The status specifies whether the key is up, down, or toggled (on, off—alternating each time the key is pressed). Above explanation as per msdn. Now the case of SHIFT button applied to these cases to a normal application as example. Now if you click the SHIFT key before calling this API in your application then SHORT nVirtAsyc = GetAsyncKeyState(VK_LSHIFT); SHORT nVirtSync = GetKeyState(VK_LSHIFT); Async = 0 Sync = 1; Now again you click SHIFT key Aync = 0 Sync = 0 Now i think you got the point, GetKeyState gives the toggle values. While GetAyncKeyState will be 1 when you have Pressed the SHIFT button. Not the released button. GetKeyState is useful for VK_NUMLOCK or VK_CAPITAL, which depends on toggle values

                  Величие не Бога может быть недооценена.

                  modified on Sunday, March 21, 2010 12:35 PM

                  R 1 Reply Last reply
                  0
                  • A Adam Roderick J

                    GetAsyncKeyState function determines whether a key is up or down at the time the function is called, and whether the key was pressed after a previous call to GetAsyncKeyState. GetKeyState function retrieves the status of the specified virtual key. The status specifies whether the key is up, down, or toggled (on, off—alternating each time the key is pressed). Above explanation as per msdn. Now the case of SHIFT button applied to these cases to a normal application as example. Now if you click the SHIFT key before calling this API in your application then SHORT nVirtAsyc = GetAsyncKeyState(VK_LSHIFT); SHORT nVirtSync = GetKeyState(VK_LSHIFT); Async = 0 Sync = 1; Now again you click SHIFT key Aync = 0 Sync = 0 Now i think you got the point, GetKeyState gives the toggle values. While GetAyncKeyState will be 1 when you have Pressed the SHIFT button. Not the released button. GetKeyState is useful for VK_NUMLOCK or VK_CAPITAL, which depends on toggle values

                    Величие не Бога может быть недооценена.

                    modified on Sunday, March 21, 2010 12:35 PM

                    R Offline
                    R Offline
                    Rozis
                    wrote on last edited by
                    #10

                    Adam Roderick J 09 wrote:

                    GetKeyState is useful for VK_NUMLOCK or VK_CAPITAL, which depends on toggle values

                    Of course you're right: there's no (practical) way to get vk_numlock with GetAsyncKeyState. I didn't thought of that - Good answer! Does this also mean that a synthesized keystroke (with SendInput() or sending keyboard messages) will not be handled with GetAsyncKeyState() ?

                    A 1 Reply Last reply
                    0
                    • A Avi Berger

                      Rozis wrote:

                      In my apps i use GetAsyncKeyState(vk_shift)!=0 to find out if the shift-key is pressed.

                      In most cases you want to use GetKeyState() and not GetAsyncKeyState(). If the shift-key was pressed when? Are you processing a particular keystroke? (Such as in an OnKeyDown handler?) GetKeyState() tells you the state at the time of the event that you are processing. GetAsyncKeyState() tells you the state at the instant that you call it. This is not coordinated with where your program is in processing its input stream. For a longer explanation, see here[^].

                      Please do not read this signature.

                      R Offline
                      R Offline
                      Rozis
                      wrote on last edited by
                      #11

                      Thanks for the link, that one is usefull...

                      1 Reply Last reply
                      0
                      • R Rozis

                        Adam Roderick J 09 wrote:

                        GetKeyState is useful for VK_NUMLOCK or VK_CAPITAL, which depends on toggle values

                        Of course you're right: there's no (practical) way to get vk_numlock with GetAsyncKeyState. I didn't thought of that - Good answer! Does this also mean that a synthesized keystroke (with SendInput() or sending keyboard messages) will not be handled with GetAsyncKeyState() ?

                        A Offline
                        A Offline
                        Adam Roderick J
                        wrote on last edited by
                        #12

                        why not? Just check with a sample code man :)

                        Величие не Бога может быть недооценена.

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups