carrot69 / keep-presence

Keeps your computer awake by moving the mouse or pressing a key when you step away.
Creative Commons Zero v1.0 Universal
246 stars 38 forks source link

force current position on move_mouse() to solve windows bug #22

Open carrot69 opened 3 months ago

m-jonas commented 3 months ago

Hi @carrot69

Thanks for looking into this. Just ran the code in this new branch but got the same error:

line 124, in move_mouse
    new_x = currentPosition[0] + delta_x
TypeError: 'NoneType' object is not subscriptable

Jonas

carrot69 commented 3 months ago

Hey @m-jonas!

Sorry, I made a mistake with the variable name. I guess I'll need to refactor since I have two variables that are called the same but one with an underscore and the other with snake case.

Can you verify now?

Thanks!

m-jonas commented 3 months ago

Hey @carrot69

I'm afraid the error still the same.

Jonas

m-jonas commented 3 months ago

I have done some troubleshooting, where I have added a few print statements:

image

This is the outcome:

image

Observe that the currentPosition tuple is deleted (?) as an object upon Windows lock.

More observations: I have tested and I believe it is pressing CTRL+ALT+DEL that kills the script somehow, and not the Windows lock action. I have tested it by pressing CTRL+ALT+DEL and then pressing ESC to go back, instead of selecting 'Lock'. I can then see the script has already failed.

carrot69 commented 3 months ago

Based on what you say and on this link: https://github.com/moses-palmer/pynput/issues/81

My best guess is that pynput returns the mouse position as None because the screen is locked.

I'm going to install Windows on a virtual machine so I can check this out and look for an elegant solution, but in the meanwhile you can try something like:

    currentPosition = mouse.position

    if currentPosition == None:
        currentPosition = (0,0)

    new_x = currentPosition[0] + delta_x
    ...

Does this work?

m-jonas commented 3 months ago

It worked!

image

Nice and simple solution :)

carrot69 commented 3 months ago

Yeah :D

I'm glad it worked!

I'm going to do a little refactoring and improve this pull request so I can push the fix to master.

Thanks for reporting this and helping debug it :)

m-jonas commented 3 months ago

No problem, glad to help!