vato
vato

Reputation: 141

How to record typing in C# (keyListener)

I want to write a simple text to speech program.

First, I want to make the program play only the written symbol. For example, if I type 'a' I want the program to say 'a' (I have recorded all of them), so when I type a word, it should spell it.

However, I am a beginner in C# and .Net and don't how to make the program understand the text I type. For example, in java I heard that there is a keyListener class, but I don't know which class should I use. I looked on MSDN but couldn't find it.

Which class or function should I use to listen to typed keys?

Upvotes: 3

Views: 10540

Answers (4)

TurmDrummer
TurmDrummer

Reputation: 160

If you are using Visual Studio like every other C# developer here is a more detailed code example:

  1. Create a Windows Form and go to the [Design].
  2. Select its properties (RMB=>properties), navigate to Events and double click LMB on KeyDown
  3. VS will create and bind the event for you
  4. Handle the KeyEventArgs depending on its value.

Example:

private void NewDialog_KeyDown(object sender, KeyEventArgs e)
{
    switch (e.KeyData)
        {
            case Keys.A:
                {
                    MethodToOutputSound(AEnum);
                    break;
                }
            case Keys.B:
                {
                    MethodToOutputSound(BEnum);
                    break;
                }
            case Keys.F11:
                {
                    DifferentMethod();
                    break;
                }
            case Keys.Escape:
                {
                    this.Close();
                    break;
                }
            default:
                {
                    break;
                }
        }
}

Or use a lot of ifs

 private void NewDialog_KeyDown(object sender, KeyEventArgs e)
 {
        if(e.KeyData == Keys.A)
        {
            MethodToOutputSound(AEnum);
        }

        if(e.KeyData == Keys.B)
        {
            MethodToOutputSound(BEnum);
        }
        ...
 }

Upvotes: 4

Moonlight
Moonlight

Reputation: 708

  private void button1_Click_1(object sender, EventArgs e)
    {
        string word = textBox1.Text;
        foreach (char i in word)
        {
            switch (i)
            {
                case 'a':
                case 'A': { // play sound a

                    break;
                }
                default:
                    {
                        // play no sound
                        break;
                    }
            }
        }
    }

Upvotes: 0

Anders Marzi Tornblad
Anders Marzi Tornblad

Reputation: 19305

Create a Windows Form with a TextBox in it. Handle the KeyPress event - that will give you the actual character that the user types. KeyDown and KeyUp won't help you.

You need to check the KeyChar property. Like this:

void MyEventHandler(object sender, KeyPressEventArgs e) {
    // Do stuff depending on the value of e.KeyChar
}

Upvotes: 0

Anand
Anand

Reputation: 14935

I suppose you are planning to use Windows Forms to achieve this. The solution would be pretty simple. These events include MouseDown, MouseUp, MouseMove, MouseEnter, MouseLeave, MouseHover, KeyPress, KeyDown, and KeyUp. Each control has these events exposed. You just need to subscribe to it.

Please refer to this http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keydown.aspx

There would be a little bit of logic to find whether a complete word has been typed or not. A simple soultion would be , when space has been pressed, you can assume a word has been completed. Its very crude logic, as the user may have typed in wrong spelling and want hit backspace and correct the spelling. You may want to add lag to it.

Upvotes: 5

Related Questions