Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
inglesuniversal opened this issue Apr 10, 2020 · 4 comments
Open

Comments

@inglesuniversal
Copy link

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

@shocknoble
Copy link

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.

@inglesuniversal
Copy link
Author

I will give it a try... stay safe

@shocknoble
Copy link

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

@shocknoble
Copy link

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])
	}
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants