TalAter/annyang

Question: How to grab the last phrase captured while HOLDING a key

inglesuniversal opened this issue · 4 comments

How get I get a string value with the last phrase captured during the seconds, that I am holding certain key on my keyboard:

`
if (e.keyCode === 84) {
*** code to grab last spoken phrase ***
var capturedPhrase = ?????

console.log('you just said: '+capturedPhrase);
}

`

Thanks

I believe the best way to do this would be to add a callback to annyang once that key's pressed event triggers, and then when that key is released, clear annyangs callback that you created. your event listener for a key being pressed would look something like this:

if (e.keyCode === 84) {
   globalArrayOfSpokenPhrases = []
   annyang.addCallback('result', function(userSaid) {
        globalArrayOfSpokenPhrases.push(userSaid);
    })

and then a listener for when that key is released where you do something along these lines:

if (e.keyCode === 84) {
    console.log(globalArrayOfSpokenPhrases);
    annyang.removeCallback('result') // removes all callbacks on the 'result' event. if you pass in a defined function instead of a lambda function, you can pass that function's name as a second parameter in the removeCallback function to remove just that callback
}

I really don't think this is perfect and I haven't tried it, but the concept should stick.

I will give it a try... stay safe

Report back if you get a chance to try it before I do! As long as I remember I will give it a crack sometime tomorrow

After a little bit of testing, this is probably easiest:

var phrases = []
var keyDown = false;

if (annyang) {
	annyang.start()
}
document.onkeydown = function(e) {
	if (e.keyCode === 83) {
		keyDown = true;
	}
}
document.onkeyup = function(e) {
	if (e.keyCode === 83) {
		keyDown = false;
		console.log(phrases);
	}
}

annyang.addCallback('result', (userSaid) => {
	console.log('Heard:', userSaid);
	if (keyDown) {
		phrases.push(userSaid[0])
	}
})