'how to detect if a key is pressed for a duration /python

def kill_my_session():
    while True:
        if keyboard.is_pressed("Esc + ctrl"):
            browser.quit()
            os._exit(0)    
        time.sleep(1)

How to make this if condition works if the Esc+Ctrl keys are pressed together for let's say 3 seconds?



Solution 1:[1]

import time
import keyboard

def kill_my_session():
    s = 0
    while True:
        if keyboard.is_pressed("Esc+ctrl"):
            time.sleep(1)
            s+=1
            if s>=3:
                print('Keys are pressed for more than 3 second.')
                s = 0


kill_my_session()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sharim Iqbal