c++ keyboard help

c++ keyboard help

Post by iamjman on Tue Apr 13, 2010 2:16 am
([msg=38190]see c++ keyboard help[/msg])

ok I have a program that runs in the background(windows) via an endless while loop that is always true, it checks my usb and sees if it needs to backup files if needed. to make it so it didnt hog any cpu i have it sleep for60 seconds,
what i would like to know is how can i make it so that it "listens" ( my mind is stuck on java's keyListener) for a key input like
ctrl + j to tell it to close or call a function. anything like that, that would tell it as soon as keys are hit, almost like key logger but detect not log..
i know how to use GetAsyncKeyState()
but it would only listen every 60seconds for a split second in my current program.

im not asking for you to write the code but maybe something to search on google or msdn
also on side note maybe a better way to make the program run in background without a while loop.. more sophisticated stuff

i know what i want to do but am limited by knowing what to look for or what its called..
any help would be appreciated
iamjman
New User
New User
 
Posts: 13
Joined: Tue Apr 22, 2008 7:19 pm
Blog: View Blog (0)


Post by echel0n on Sun Jul 04, 2010 2:45 pm
([msg=41266]see [/msg])

I don't think you need to make it sleep for 60 seconds on a modern CPU, a couple of milliseconds might be enough to do the trick. Try Sleep()-ing for 1ms, and if that doesn't use too much CPU go with it, else try with sleeping for some more milliseconds.

Then, when you're at about 5ms of sleep, you might do it as (pseudo-code):
Code: Select all
loop{
      Sleep for 5ms
      CheckForKey
      If key was pressed{
             The rest of the code
      }
}
echel0n
New User
New User
 
Posts: 1
Joined: Sun Jul 04, 2010 2:37 pm
Blog: View Blog (0)


Re: c++ keyboard help

Post by Mrfred on Sun Sep 12, 2010 6:29 pm
([msg=45606]see Re: c++ keyboard help[/msg])

I am unsure if this is the kind of thing that you wanted, but you can set a keyboard shortcut if you want to start running a program.

This webpage gives you the information that you need to start a program using keyboard shortcuts.
Mrfred
New User
New User
 
Posts: 3
Joined: Sun Sep 12, 2010 5:20 pm
Blog: View Blog (0)



Return to C and C++

Who is online

Users browsing this forum: No registered users and 0 guests