Project 4: Speaking keyboard

This article is the extension for project 3. Please visit it before starting this for better understanding.

In the last article, we have shown keyboard and printed clicked letter in console. We are going to do following tasks in this article:

Task 1:…


This content originally appeared on DEV Community and was authored by DEV Community

This article is the extension for project 3. Please visit it before starting this for better understanding.

In the last article, we have shown keyboard and printed clicked letter in console. We are going to do following tasks in this article:

Task 1: Speak the clicked letters
Task 2: Print letters just below the keyboard instead in console.

Here is the whole code:

<html>

<body>
</body>
<script>

    for (let i = 65; i <= 90; i++) {
        const button = document.createElement('button');
        const char = String.fromCharCode(i);
        const span = document.createElement('span');
        span.style.fontSize = '50px';
        span.appendChild(document.createTextNode(char));
        button.appendChild(span);
        document.body.appendChild(button);
        button.setAttribute('id', char);
        button.style.padding = '30px';
        button.style.margin = '10px';
        button.onclick = function () { getLetter(char) };
    }
    function getLetter(id) {
        const speech = new SpeechSynthesisUtterance();
        const letter = document.getElementById(id).textContent;
        speech.text = letter;
        window.speechSynthesis.speak(speech);
        const p = document.createElement('p');
        p.style.fontSize = '30px';
        p.innerHTML = letter;
        document.body.appendChild(p);
    }
</script>

</html>

We have pretty much seen what are we doing inside for loop in project 3.

We don't need to reinvent the wheel here. Let's jump and understand how we can achieve Task 1 & 2 now.

getLetter function gets triggered when user clicks on any letter in keyboard.

so, understanding what's inside of it would be suffice.

const speech = new SpeechSynthesisUtterance();
        const letter = document.getElementById(id).textContent;
        speech.text = letter;
        window.speechSynthesis.speak(speech);

The above snippet is responsible for speaking the clicked letter.

SpeechSynthesisUtterance is the class instantiated in first line. Then, we captured textContent of the letter clicked. Assign that to speech to inform synthesisUtterance which text to speech. After that, use speak function to speak the word. We are done with Task 1.

Let's move on to Task 2 i.e. printing the clicked item just below the keyboard itself. It's quite straightforward.

const p = document.createElement('p');
p.style.fontSize = '30px';
p.innerHTML = letter;
document.body.appendChild(p);

creating p element with fontSize as 30px for better view. innerHtml sets letter to p and appendChild appends p to document body.

Now, if you click on any letter which will be uttered and displayed on web page.

Thank You :) Happy Reading!

💎 Love to see your response

  1. Like - You reached here means. I think, I deserve a like.
  2. Comment - We can learn together.
  3. Share - Makes others also find this resource useful.
  4. Subscribe / Follow - to stay up to date with my daily articles.
  5. Encourage me - You can buy me a Coffee

Let's discuss further.

  1. Just DM @urstrulyvishwak
  2. Or mention
    @urstrulyvishwak

For further updates:

Follow @urstrulyvishwak


This content originally appeared on DEV Community and was authored by DEV Community


Print Share Comment Cite Upload Translate Updates
APA

DEV Community | Sciencx (2022-02-26T07:07:43+00:00) Project 4: Speaking keyboard. Retrieved from https://www.scien.cx/2022/02/26/project-4-speaking-keyboard/

MLA
" » Project 4: Speaking keyboard." DEV Community | Sciencx - Saturday February 26, 2022, https://www.scien.cx/2022/02/26/project-4-speaking-keyboard/
HARVARD
DEV Community | Sciencx Saturday February 26, 2022 » Project 4: Speaking keyboard., viewed ,<https://www.scien.cx/2022/02/26/project-4-speaking-keyboard/>
VANCOUVER
DEV Community | Sciencx - » Project 4: Speaking keyboard. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/02/26/project-4-speaking-keyboard/
CHICAGO
" » Project 4: Speaking keyboard." DEV Community | Sciencx - Accessed . https://www.scien.cx/2022/02/26/project-4-speaking-keyboard/
IEEE
" » Project 4: Speaking keyboard." DEV Community | Sciencx [Online]. Available: https://www.scien.cx/2022/02/26/project-4-speaking-keyboard/. [Accessed: ]
rf:citation
» Project 4: Speaking keyboard | DEV Community | Sciencx | https://www.scien.cx/2022/02/26/project-4-speaking-keyboard/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.