TalAter / annyang

:speech_balloon: Speech recognition for your site
https://www.talater.com/annyang/
MIT License
6.57k stars 1.05k forks source link

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

Open inglesuniversal opened 4 years ago

inglesuniversal commented 4 years ago

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 commented 3 years ago

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 commented 3 years ago

I will give it a try... stay safe

shocknoble commented 3 years ago

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 commented 3 years ago

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