Sleep() Question - previously about key input

Started by
6 comments, last by v0dKA 18 years, 1 month ago
I want to run my program in the background and be able to have it perform a certain action when a certain key combination is pressed - regardless of which window is currently active. What can I use to accomplish this? Also, is it possible to create this in a console application, without the formal message loop of a Windows program? [Edited by - v0dKA on March 13, 2006 6:53:51 PM]
.:<<-v0d[KA]->>:.
Advertisement
Assuming you're using Windows, I believe you can use GetAsyncKeyState(key#) to get a key press regardless of wether or not the window has the focus.
Thanks, I should have really thought of trying that =)

Instead of making a new thread, allow me to ask an unrelated question here.

I plan to have an infinite loop in my program to catch the aforementioned key combination. However, I don't want it to hog up all the resources. Currently, when I run it, it takes up around 50% of the CPU (I have hyperthreading on this machine). The program hardly needs that much processing power. I believe the solution to this would be to add a Sleep( 0 ) inside the infinite loop, no? I tried that, but the memory usage seems to be unaffected... Why not? Should I be concerned about having my little (less than 1MB) program take so much processing power?
.:<<-v0d[KA]->>:.
Memory usage is different than CPU usage.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
If you pass zero to Sleep the function gives up whatever time it's got left to another thread. If such a thread isn't present, Sleep(0) returns instantly...

Instead, use Sleep(1) or anything greater...
Another quick question for which an entire thread is probably not necessary:

How can I determine if a given HWND (call it hWnd) is currently active?
.:<<-v0d[KA]->>:.
If you want to check whether or not a certain window is on the top of the Z-Order, you can call GetForegroundWindow().

[Edited by - raz0r on March 13, 2006 7:27:34 PM]
Thank you, I have everything I need for my program now =)
.:<<-v0d[KA]->>:.

This topic is closed to new replies.

Advertisement