key pressed during an interval of time

Official forum for open source FreePIE discussion and development.
Post Reply
bvaliasa
One Eyed Hopeful
Posts: 2
Joined: Mon Jul 13, 2015 3:04 am

key pressed during an interval of time

Post by bvaliasa »

Hello,

I try to create a simple script that should be able to detect the number of time I pressed a key during an interval of time.

Code: Select all

import threading

def hello():
    speech.say(str("yes " + counter2))
    counter2 = 0

if starting:
	speech.say("Hello world")
	counter2 = 0 
    
if keyboard.getPressed(Key.F4):
    if counter2 == 0:
        speech.say("Timer")
    	global t
    	t = threading.Timer(1.0, hello) 
        t.start()
        threading.Event.wait()
    counter2 = counter2 + 1
For some reason my timer is never launched. Could you help me ?
bvaliasa
One Eyed Hopeful
Posts: 2
Joined: Mon Jul 13, 2015 3:04 am

Re: key pressed during an interval of time

Post by bvaliasa »

I solved the issue myself

Code: Select all

from System.Timers import Timer

def update(s, e):
   global counter
   diagnostics.debug("Pressing key " + str(counter))
   counter = 0
   t.Stop()

if starting:    
   t = Timer(250)
   t.Elapsed += update 
   counter = 0
  
if stopping:
   cancel()

def cancel():
   diagnostics.debug("canceling")
   t.Stop()

if keyboard.getPressed(Key.F4):
   t.Stop()
   counter = counter + 1
   t.Start()
This script allow me the count the numer of time I press a key until I stop to press it during 250 ms.
Post Reply

Return to “FreePIE”