keypress detection in python without hijacking the keypress

Viewed 37

I've a program to detect mouse pointer movement and move the mouse if pointer is stationary during some period. I'm using pyautogui for this purpose.
The reason for doing this is to avoid the screen being locked and I hate to type the password again and again. (No I don't have control over the settings)
This is my code for that.

import pyautogui as screen
from time import sleep

startX, startY = 1760, 10
tempX, tempY = startX, startY
while True:
    posX, posY = screen.position()
    if tempX == posX or tempY == posY:
        screen.moveTo(startX, startY)
        sleep(1)
        screen.moveTo(posX, posY)
    sleep(180)
    tempX, tempY = posX, posY

The above code works like a charm for mouse pointer movement detection. But while long keyboard usage sessions (like coding), there is mouse pointer movements because of the above snippet and where ever the pointer goes, if there is some hover action available, that pops out.

So is there anyway to detect whether any keys have been pressed during the sleep time? I don't want to hijack the keypresses as input to python program and also don't want to actively monitor the keypresses. Just some buffer like thing which will keep on mirroring the keypresses (without hijacking) and when I wake up from sleep, check the buffer if any content is there or not and take action accordingly.

P.S.
The above code is cross platform but being used in a unix environment currently. So if any solution specific to unix will also be acceptable.

0 Answers
Related