4

So im in C# Windows Form Application in Visual Studio, Im making a soundboard program that plays sounds to corresponding "Hotkeys" that are set by the user. Im having trouble figuring out how to get the program to recognise the user set hotkeys. That might sound vague, so let me show you.

Ok, this is the code that is set to run from the start "What you edit when you double click the form in the design menu"

    if (e.KeyCode == Keys.D)

    {
     // The Program plays sound
    }

So I know that when I press "d" on my keyboard, the sound plays.

Well, I want the sound to play when I type in a character that I have written in a textbox somewhere else. So like I open my program put "t" into the "set hotkey" text box, then press t and the sound plays.

So I want the program do do something like this

    if (e.KeyCode == Keys.(textBox1.Text)) // textBox1 is my set hotkey box 

    {
     // The Program plays sound
    }

Is there some way I can get textBox.Text to be recognised by e.KeyCode? Do I have to convert textBox1.Text to a key code value? If so, How?

Im new to coding, so try to explain it to me as if I were a dummy :D

I really appriciate your time!

3 Answers3

5

Use Enum.TryParse method

Keys key;
if (Enum.TryParse<Keys>(textBox1.Text, out key))
{
    //Do something with key
    if (e.KeyCode == key)
    {

    }
}

If you're concerned about only 1 character try the following:

Keys key;
if (textBox1.TextLength > 0 && Enum.TryParse<Keys>(textBox1.Text[0].ToString(), out key))
{
    //Do something with key
    if (e.KeyCode == key)
    {

    }
}
Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
  • If i put that in, i get red line from "Enum to the end of line. Error reads, "The Best Overloaded Method Match for 'System.enum.tryparse(string, out system.windows.forms.keys)' has some invalid arguements – David Lancaster Aug 01 '14 at 07:21
  • @DavidLancaster Am sorry, I can't help you anymore if you don't say what is the error message you get. Also which version of .net you're using? – Sriram Sakthivel Aug 01 '14 at 07:23
  • I edited for error message. Im using visual studio 2012 – David Lancaster Aug 01 '14 at 07:26
  • enum.tryparse is only available in .Net framework 4 or above. – Sumeshk Aug 01 '14 at 07:29
  • Enum.tryparse expect 3 argument so use if(Enum.TryParse(ch.ToString,true, out key)) and let me know the result – Sumeshk Aug 01 '14 at 07:34
  • Hmm, that must be issue. Is there a method for a lower framwork? – David Lancaster Aug 01 '14 at 07:34
  • @DavidLancaster You can implement Tryparse on your own [like this](http://stackoverflow.com/questions/15017151/implementation-of-enum-tryparse-in-net-3-5) – Sriram Sakthivel Aug 01 '14 at 07:35
  • Which frame work are you using..?? you can check it from the properties window of your project. – Sumeshk Aug 01 '14 at 07:36
  • says framework 4.5 also, now line uner "e.Keycode" reads "'system.eventArgs' does not contain definition for KeyCode and no extension method 'Keycode' accepting first arguement of type 'system.eventArgs' could be found – David Lancaster Aug 01 '14 at 07:49
  • @DavidLancaster `e.KeyCode` is taken from your code. If you don't need, you can ignore it. `key` is the converted value. Use it. – Sriram Sakthivel Aug 01 '14 at 07:52
1

Try to parse the text value as enum.

if (e.KeyCode == (Keys)Enum.Parse(typeof(Keys), textBox1.Text))
{
 // The Program plays sound
}

(Pseudo code, not complied)

shieldgenerator7
  • 1,507
  • 17
  • 22
Sham
  • 830
  • 9
  • 27
0

You could try to create an KeyDown event at your textbox and with an switch staement you can differ between the different keys

    private void textBox1_KeyDown(object sender, KeyEventArgs e)
    {
        Switch(e.Key)
             case e.Key == Key.D; PlaysondforD; break;
             case e.Key == Key.E; PlaysondforE; break;
             .........
    }
chris
  • 57
  • 1
  • 1
  • 9