Receive events on inactive Windows?

Windows specific forum
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

>> Repeat : Sleep_(1) : Until GetAsyncKeyState_(#VK_F1)<>0
>
> a bad loop, it will most likely eat processortime

Well, 0% CPU isn't much processor time for such a tight loop. ;)
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Edwin Knoppert
Addict
Addict
Posts: 1073
Joined: Fri Apr 25, 2003 11:13 pm
Location: Netherlands
Contact:

Post by Edwin Knoppert »

DoubleDutch wrote:
Can be, therefore it remains a bad scenario..
I don't understand what you mean, the result will mean that your request has failed - so pick another key.

What does "Can be, therefore it remains a bad scenario" mean???

-Anthony
I said using HK's is a bad approach since it can interfere or as you state fail to initialize die existing HK.
That sounds as a poor scenario if this part is failing for some reason.
are you gonna present the end-user with a dumb message like "sorry hotkey does not work, bye"

Iow, your program should always work (unless it's for hobby)

Clear?
Edwin Knoppert
Addict
Addict
Posts: 1073
Joined: Fri Apr 25, 2003 11:13 pm
Location: Netherlands
Contact:

Post by Edwin Knoppert »

PB wrote:>> Repeat : Sleep_(1) : Until GetAsyncKeyState_(#VK_F1)<>0
>
> a bad loop, it will most likely eat processortime

Well, 0% CPU isn't much processor time for such a tight loop. ;)
Hmm, i don't know how this behavious exactly.
If GetAsyncKeyState_() is holding the app.. it would do indeed.
But, if not you are continously in a loop instead, and should eat cpu time.
:)
User avatar
DoubleDutch
Addict
Addict
Posts: 3220
Joined: Thu Aug 07, 2003 7:01 pm
Location: United Kingdom
Contact:

Post by DoubleDutch »

You could say "hotkey already allocated", then go to you dialog to set up a new hot key?
https://deluxepixel.com <- My Business website
https://reportcomplete.com <- School end of term reports system
Edwin Knoppert
Addict
Addict
Posts: 1073
Joined: Fri Apr 25, 2003 11:13 pm
Location: Netherlands
Contact:

Post by Edwin Knoppert »

DoubleDutch wrote:You could say "hotkey already allocated", then go to you dialog to set up a new hot key?
That really depends on the app, isn't?
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

Try the GetAsyncKeyState loop and check it with any CPU monitor... they
all report 0% CPU usage, due to Sleep in the middle. If you remove the
Sleep, the CPU jumps up to 99% usage immediately. Remember, it's a
tiny loop that doesn't do much at all -- why would you expect the CPU to
work overtime with it? The loop does hardly anything at all. :)
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Edwin Knoppert
Addict
Addict
Posts: 1073
Joined: Fri Apr 25, 2003 11:13 pm
Location: Netherlands
Contact:

Post by Edwin Knoppert »

A loop is a loop.
After your post i expected an odd behaviour like Windows waits in the api for a key.
(I had some trouble with the KB api's in the past so therefore me thinking this way :) )

You are lucky the sleep is preventing blocking and as you say seems to keep the app idle... somehow.
For myself i would not rely on that behaviour under different os's.
Sleep is not the same as calling a specially crreated api as used in a windows msgpump: GetMessage()

How windows is keeping the loop/app idle in this call is their way and works as expected.

Maybe less understood, the msgpump loop is always stuck in the GetMessage() API *except* when a message needs to be executed.
Then it will be released and your loop will do a new cycle.

I know, this is not related directly but i wanted to explain sleep is different.
Edwin Knoppert
Addict
Addict
Posts: 1073
Joined: Fri Apr 25, 2003 11:13 pm
Location: Netherlands
Contact:

Post by Edwin Knoppert »

Hmm, i just read about the sleep API and it seems to do what you want:

>A value of zero causes the thread to relinquish the remainder of its time slice to any other thread of equal priority that is ready to run.

etc..

Well, you may be in luck :)
KameHameHaaa
User
User
Posts: 19
Joined: Wed Oct 05, 2005 2:44 pm

Post by KameHameHaaa »

I have to intercept any mouse movement, keyboard press, to launch an event after some time of inactivity, just like the task that launches a screensaver...

Is it possible to use GetAsyncKeyState for any key instead to check ANY possible key?
What about mouse movement?

Thanks a lot.
mp303

Post by mp303 »

KameHameHaaa wrote:I have to intercept any mouse movement, keyboard press, to launch an event after some time of inactivity, just like the task that launches a screensaver...
me too!
KameHameHaaa wrote: Is it possible to use GetAsyncKeyState for any key instead to check ANY possible key?
What about mouse movement?
I found that you can capture mouse movement globally with DesktopMouseX() and DesktopMouseY() - even when the application looses focus, this seems to continue to work. (at least on Windows, haven't had the opportunity to test with Linux)

I'm searching high and low for a global keyboard hook myself - I wish it could be built into PB, as the time-tracker application I need to write has to work both on Linux and Windows, so a Windows-specific implementation does me little good... (of course, a library would do nicely too, but I don't know anyone with the Linux skills)
Post Reply