CyberVillain wrote:added a stopWatch method that will be added in next version¨
Code: Select all
if filters.stopWatch(keyboard.getKeyDown(Key.A), 5000):
diagnostics.debug("Key down for 5 sec")
How does Glovepie work, does it require you to release the button until it starts the timer again? My code curretly does not requiore this so if you hold Key.A down it will print the message each 5 seconds.
Sorry, I've never used GlovePIE before but I'm indeed having problems replicating a key hold.
I've tried 2 aproaches that didn't work
1.- Using "keyboard.setKey( k, True )" once and "keyboard.setKey( k, False )" once after N seconds.
2.- Using "keyboard.setKey( k, True )" on every FreePIE pass while the button is supposed to be held down and then "keyboard.setKey( k, False )" once after N seconds.
None of them are working :S though I haven't tested it in any game, only looking at the keys written on a notepad.
Currently the code I'm using to hold a button looks like this:
There are 3 classes there:
- KeyAction is an abstract class to store a list of keys and define "execute" and "update" functions
- execute: is a function that will be called when a certain command is said
- update: will be called on every pass and will update the action if needed (f.i. releasing the a key)
-KeyHold is a class derived from KeyAction
-VoiceCommand is the class that stores and handles the speech commands and responses
Code: Select all
import time;
# ***********************************
# Key Actions & VoiceCommand Classes
# ***********************************
class KeyAction:
def __init__(self, key):
self.keys = []
# append or extend (both are valid)
if isinstance(key, list):
self.keys.extend(key)
else:
self.keys.append(key)
def setKeyDown(self):
for key in self.keys:
keyboard.setKeyDown(key)
def setKeyUp(self):
for key in self.keys:
keyboard.setKeyUp(key)
def setKey(self, down):
for key in self.keys:
keyboard.setKey(key, down)
def setKeyPressed(self):
for key in self.keys:
keyboard.setPressed(key)
def execute(self):
raise NotImplementedError("Subclass must implement execute method")
def update(self, curentTime):
pass
class KeyHold(KeyAction):
def __init__(self, key, duration):
KeyAction.__init__(self, key)
self.duration = duration
self.time = time.time()
self.needUpdate = False
def execute(self):
# set the keys down
self.setKey(True)
# start the update timer
self.time = time.time()
self.needUpdate = True
def update(self, currentTime):
if self.needUpdate:
# determine if we should stop pressing the keys
if (currentTime - self.time) >= self.duration:
self.needUpdate = False
diagnostics.debug("Releasing")
self.setKeyUp(False)
else:
diagnostics.debug("Holding")
self.setKey(True)
class VoiceCommand:
def __init__(self, cmd, response, action = None):
self.cmd = cmd
self.response = response
self.action = action
def said(self, confidence, response=False):
return ((self.cmd != "") and speech.said(self.cmd, confidence))
def playResponse(self):
if self.response:
speech.say(self.response)
def execute(self):
if self.action:
self.action.execute()
def update(self, currentTime):
if self.action:
self.action.update(currentTime)
# ***********************************
# Config and commands
# ***********************************
if starting:
confidenceLevel = 0.7
commands = [
VoiceCommand("Testing",
"Holding Button",
KeyHold( Key.A, 2 ))
]
# ***********************************
# Main Loop
# ***********************************
currentTime = time.time()
for command in commands:
# if said execute action
if command.said(confidenceLevel):
command.playResponse()
command.execute()
command.update(currentTime)