Zhorn Software
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Go down
avatar
SleepySpider
Guest

Caffeine - Only activate when there's no mouse/keyboard activity Empty Caffeine - Only activate when there's no mouse/keyboard activity

Tue Dec 07, 2021 5:56 pm
I would like a feature added to caffeine where it only becomes active when it notices that there's no mouse or keyboard activity for x minutes.
Once there's activity, it deactivates. The reason is because pressing keys and simulating keypresses can cause issues in some apps.
For example in cygwin, F15 produces a ~ character for some reason.
Admin
Admin
Admin
Posts : 522
Join date : 2018-03-30
Location : London
http://www.zhornsoftware.co.uk

Caffeine - Only activate when there's no mouse/keyboard activity Empty Re: Caffeine - Only activate when there's no mouse/keyboard activity

Sun Jan 02, 2022 1:40 pm
This won't happen, but I can explain why.

In order for Caffeine to know whether there's any keyboard or mouse activity, it would need to get to see every key press so it could know that the presses are happening.

Apps which ask for a preview of every key press which takes place on the system look like keyloggers .. and who wants to run an app which looks like a keylogger.

Tom
avatar
orev
Posts : 3
Join date : 2023-07-10

Caffeine - Only activate when there's no mouse/keyboard activity Empty Re: Caffeine - Only activate when there's no mouse/keyboard activity

Mon Jul 10, 2023 8:09 pm
I came here to suggest the same feature, however I disagree with the reasoning why it will not be implemented. The software does not need to become a keylogger, it can use the Windows function GetLastInputInfo() to check when the last input event was received. This does not give access to any keyboard data, only a timestamp.

The logic would be: before sending the keep-awake keypress, check GetLastInputInfo, don't send the key if it's less than the timeout value, and reset the timer and start counting again.
Admin
Admin
Admin
Posts : 522
Join date : 2018-03-30
Location : London
http://www.zhornsoftware.co.uk

Caffeine - Only activate when there's no mouse/keyboard activity Empty Re: Caffeine - Only activate when there's no mouse/keyboard activity

Fri Jul 14, 2023 8:50 pm
Ah interesting, I didn't know about this API call, thanks.  I'll take a look.

Tom

orev likes this post

Admin
Admin
Admin
Posts : 522
Join date : 2018-03-30
Location : London
http://www.zhornsoftware.co.uk

Caffeine - Only activate when there's no mouse/keyboard activity Empty Re: Caffeine - Only activate when there's no mouse/keyboard activity

Sun Jul 16, 2023 1:18 pm
Unfortunately this won't work.  The GetLastInputInfo() API call is also "fooled" by the SendInput() call which Caffeine uses.  I've got both running now, with Caffeine firing every couple of seconds, and the test rig I put together containing the GetLastInputInfo() call thinks that there has been user input when Caffeine fires SendInput().

This means that adding the GetLastInputInfo() call to Caffeine will mean it always thinks that the user is present whether it's genuine input or Caffeine faked.

Tom
avatar
orev
Posts : 3
Join date : 2023-07-10

Caffeine - Only activate when there's no mouse/keyboard activity Empty Re: Caffeine - Only activate when there's no mouse/keyboard activity

Thu Jul 27, 2023 11:32 pm
To me, the potential to avoid stepping on real user activity (i.e. Caffeine sends a SHIFT while the user is typing) would be pretty important. I'm pretty sure I've had this issue before when typing, and there's always the chance it can cause other unintended side effects. I prefer using SHIFT as F15 interferes with PuTTY too much.

Is there a reason why Caffeine needs to be able to differentiate between "real" user input and input that's generated by Caffeine? That's the only reason I can think of on why this would cause a problem. Just wondering--you're the expert.
Admin
Admin
Admin
Posts : 522
Join date : 2018-03-30
Location : London
http://www.zhornsoftware.co.uk

Caffeine - Only activate when there's no mouse/keyboard activity Empty Re: Caffeine - Only activate when there's no mouse/keyboard activity

Tue Aug 01, 2023 8:59 pm
To avoid sending fake input when the user is really present and pressing buttons, the app would need to be able to tell whether detected input was caused by the user pressing a button or whether it's been generated by itself.

Seeing as that API call measures the time since there was either sort of input, Caffeine can't tell the difference with that call.

One way which that could be technically possible would be for the app to install a global keyboard hook, and that's not something to be done lightly.  Heck I'm not sure without trying it, maybe even a keyboard hook would be fooled by the fake keypresses Caffeine sends.

Have you tried using the -stes command line option, which doesn't fake input, but instead uses another method to prevent the machine from sleeping?  Maybe that would work ok for you?

Tom
avatar
orev
Posts : 3
Join date : 2023-07-10

Caffeine - Only activate when there's no mouse/keyboard activity Empty Re: Caffeine - Only activate when there's no mouse/keyboard activity

Thu Aug 17, 2023 12:06 am
I would expect that Caffeine would only care about checking for user input within the last timeout window. Does it need to know the total time the user has been away? If there's been real user activity, GetLastInputInfo() would return a value less than the Caffeine timeout value, and if the user is still away, it would return exactly the Caffeine timeout value (plus or minus some milliseconds). As long as Caffeine checks the GetLastInputInfo() value before sending the keep-awake key (and doesn't send it if the value is less than the timeout), it should never conflict with real user input.

Here's some naive pseudo-code:


Code:
# Always send awake key when the timer expires
timeout = 60
while true    # run forever
    for i=0; i<timeout; i++
        if i >= timeout
            SendAwakeKey
            i=0
        sleep 1


Code:
# Only send awake key when user is actually idle as long as the timeout
timeout = 60
while true    # run forever
    if (CurrentTime - GetLastInputInfo) >= timeout
        SendAwakeKey
    sleep 1



I'm sure there's probably a lot more going on in your code, but this is the concept of what I'm getting at.
Sponsored content

Caffeine - Only activate when there's no mouse/keyboard activity Empty Re: Caffeine - Only activate when there's no mouse/keyboard activity

Back to top
Permissions in this forum:
You can reply to topics in this forum