Amr
Amr

Reputation: 115

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?

Upvotes: 0

Views: 61

Answers (1)

Sharim09
Sharim09

Reputation: 6224

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()

Upvotes: 1

Related Questions