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. Is there any alternative for GetAsyncKeyState( int ) function in visual c++?

Is there any alternative for GetAsyncKeyState( int ) function in visual c++?

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
5 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.
  • N Offline
    N Offline
    Nilesh Hamane
    wrote on last edited by
    #1

    I am creating an background application which runs in the background, for that i want to know which key has been pressed by user. Right now, i am using this code:

    while( 1 )
    {
    for( int i = 0; i < 256; i++ )
    {
    GetAsyncKeyState( i );
    }
    }

    But, due to while( 1 ) other applications are hanging. Is there any alternative for GetAsyncKeyState( int ) function OR can i use the same function in another way?

    C C 2 Replies Last reply
    0
    • N Nilesh Hamane

      I am creating an background application which runs in the background, for that i want to know which key has been pressed by user. Right now, i am using this code:

      while( 1 )
      {
      for( int i = 0; i < 256; i++ )
      {
      GetAsyncKeyState( i );
      }
      }

      But, due to while( 1 ) other applications are hanging. Is there any alternative for GetAsyncKeyState( int ) function OR can i use the same function in another way?

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

      You should use a global keyboard hook (see: "Using Hooks") for the purpose. Are you writing a keylogger? :)

      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]

      N 1 Reply Last reply
      0
      • C CPallini

        You should use a global keyboard hook (see: "Using Hooks") for the purpose. Are you writing a keylogger? :)

        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]

        N Offline
        N Offline
        Nilesh Hamane
        wrote on last edited by
        #3

        Thanks for your reply Pallini. I was curious about how keylogger works and while searching, i got a keylogger code in c++. Now i am just trying to edit it, just for a learning purpose. :|

        R 1 Reply Last reply
        0
        • N Nilesh Hamane

          Thanks for your reply Pallini. I was curious about how keylogger works and while searching, i got a keylogger code in c++. Now i am just trying to edit it, just for a learning purpose. :|

          R Offline
          R Offline
          Rajesh R Subramanian
          wrote on last edited by
          #4

          Here's some neat theory on hooks and DLLs and a practical example to illustrate it all, by Dr. Joseph Newcomer: Hooks and DLLs[^]

          “Follow your bliss.” – Joseph Campbell

          1 Reply Last reply
          0
          • N Nilesh Hamane

            I am creating an background application which runs in the background, for that i want to know which key has been pressed by user. Right now, i am using this code:

            while( 1 )
            {
            for( int i = 0; i < 256; i++ )
            {
            GetAsyncKeyState( i );
            }
            }

            But, due to while( 1 ) other applications are hanging. Is there any alternative for GetAsyncKeyState( int ) function OR can i use the same function in another way?

            C Offline
            C Offline
            Code o mat
            wrote on last edited by
            #5

            I see you already found a better way to do what you want, so this is just an "additional footnote" to your original problem: to make your while loop less CPU hungry you can add a Sleep(1) call somewhere inside it, or maybe use SwithToThread[^] (althorough SwitchTothread seems to be much less effective than Sleep), this way the loop yields execution to other threads and does not eat up all the CPU power. I mean something like:

            while( 1 )
            {
            for( int i = 0; i < 256; i++ )
            {
            GetAsyncKeyState( i );
            }
            Sleep(1);
            }

            > The problem with computers is that they do what you tell them to do and not what you want them to do. < > Sometimes you just have to hate coding to do it well. <

            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