开发者

I'm capturing keys on my WinForm but I'm trying to see if the pressed key is a 'String' - getting an exception

开发者 https://www.devze.com 2022-12-28 04:52 出处:网络
Here\'s my code: void gkh_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == neededLetter as Keys)

Here's my code:

        void gkh_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == neededLetter as Keys)
            {
             开发者_JS百科   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.


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.


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


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
  }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消