tags:

views:

53

answers:

3

Here's my code:

        void gkh_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == neededLetter as Keys)
            {
                if (neededLetter == "n")
                {
                    neededLetter = "o";
                }
                else if (neededLetter == "o")
                {
                    neededLetter = "t";
                }
                else if (neededLetter == "t")
                {
                    neededLetter = "e";
                }
                else if (neededLetter == "e")
                {
                    this.Show();
                }
            }
            else
            { 
                neededLetter = "n";
            }
        }

I'm getting an error on the first If block:

The as operator must be used with a reference type or nullable type

Edit: To be clearer:

User presses a key and the computer compares that e.Keycode to a string variable with the letter "n" set to it. If true, set the variable neededLetter to "o".

After that, when a user pressed another key if it is "o", save neededletter to "t". and so on and so forth.

A: 

KeyEventArgs.KeyCode is an enumeration value. From the MSDN documentation:

private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
    // Determine whether the key entered is the F1 key. If it is, display Help.
    if(e.KeyCode == Keys.F1 && (e.Alt || e.Control || e.Shift))
    {
        // Display a pop-up Help topic to assist the user.
        Help.ShowPopup(textBox1, "Enter your name.", new Point(textBox1.Bottom, textBox1.Right));
    }
    else if(e.KeyCode == Keys.F2 && e.Modifiers == Keys.Alt)
    {
        // Display a pop-up Help topic to provide additional assistance to the user.
        Help.ShowPopup(textBox1, "Enter your first name followed by your last name. Middle name is optional.",
            new Point(textBox1.Top, this.textBox1.Left));
    }
}
Nathan Taylor
I know that; I don't know how to compare an Enum value to a string variable.
Serg
I apologize for the confusion. If it has to be a string you could call `e.KeyCode.ToString() == "e"`, but I recommend comparing them as enumerations rather than strings.
Nathan Taylor
Thanks for the help, but I'm going to capture the input 'note'. Meaning a user will first type n, then o, then t, then e; and then my window will show. That's why I need to compare the e.Keycode of the pressed key to a local string variable. :)
Serg
A: 

Use this:

if (e.KeyCode == (Keys)neededLetter[0])

Perhaps you should make neededLetter as char type, so you could use:

if (e.KeyCode == (Keys)neededLetter)

Or better yet, just make neededLetter as Keys type, so you could just do this:

if (e.KeyCode == neededLetter)
{
    if (neededLetter == Keys.N)
        neededLetter = Keys.O;
    else if (neededLetter == Keys.O)
        neededLetter = Keys.T;

    ...
}

@Sergio, re: one key input only:

Yeah it accept only one keyboard input. In fact if you want to interpret key combinations like Ctrl+A, you could not catch both of them in e.KeyCode simultaneously, only the last pressed key(regular key or otherwise(key modifiers)) registers in e.KeyCode. If there are no key modifier boolean properties like e.Control, e.Shift, e.Alt, we will not be able to read keyboard shortcuts, and we will have to roll our own routine for setting a state variable and detecting if those keyboard modifiers is/are still hold by the user. But luckily there's a built in property that indicates that modifiers are still hold by the user, so we can do this:

if (e.KeyCode == Keys.A && e.Control)
   MessageBox.Show("Test");

And to test that e.KeyCode only register the last key regardless if it is key(A) or key modifier(Ctrl). Try to hold A(don't release), then press Ctrl, the word "Test" will not pop-up on screen, the last pressed(Ctrl) is also registered on e.KeyCode. But if you will hold Ctrl then A, the word "Test" will pop-up on screen, e.Control is a state that indicates if Ctrl is still hold by the user.

Michael Buen
This worked, but I wonder why... Does Keys only accept a single char?
Serg
@Sergio: `Keys` is an enumeration representing a single key on the keyboard (not counting modifier keys). So yes, `Keys` is only for a single character.
Adam Robinson
@Adam Robinson: on the contrary, modifiers are also included in Keys enum, see my response to Sergio, key modifiers are also registered to e.KeyCode(`Keys` type). or maybe i misread your sentence
Michael Buen
@Michael: That's what I meant by "single key on the keyboard (not counting modifier keys)". Sorry if that was confusing.
Adam Robinson
A: 

You could do this with a bit simpler logic using KeyPress instead, like this:

string buffer = ""; //buffer to store what the user typed: n, no, not, etc...
void gkh_KeyPress(object sender, KeyPressEventArgs e)
{
  buffer += e.KeyChar; //add key to the buffer, then check if we've made progress
  if (buffer.IndexOf("note") > -1) {
    this.Show(); //"notes" matched completely
    buffer = ""; //reset for another run
  }
  else if ("note".IndexOf(buffer) != 0) {
    buffer = ""; //Another key not in sequence was hit
  }
}
Nick Craver