locked
Detect whether a key has been pressed RRS feed

  • Question

  • Hello all,

    I'm making a calculator, fairly simple. It works perfectly, but I dont know how to use keyboard keys. What I mean is, if you're going to use the calculator, you have to actually press the buttons, you cant just press a key on your keyboard. I've done various searches and come up with nothing. If anyone could help me out, I'd be much obliged.
    Friday, July 3, 2009 5:32 PM

Answers

  • You need to add a KeyDown event handler to your calculator form. Something like :

    calcForm.KeyDown += KeyDownHandler; <-- where that's the name of your event handler method
    http://blog.voidnish.com
    • Marked as answer by TheBlueCard Friday, July 3, 2009 7:04 PM
    Friday, July 3, 2009 6:54 PM

All replies

  • Assuming this is WinForms, have you looked at the KeyUp, KeyDown, and KeyPress events?
    http://blog.voidnish.com
    Friday, July 3, 2009 5:52 PM
  • I have, but I dont understand them.
    Friday, July 3, 2009 5:57 PM
  • The event handler receives a KeyEventArgs object which has a KeyCode property. That's what you need to process.
    http://blog.voidnish.com
    Friday, July 3, 2009 6:06 PM
  • private void KeyPress(object sender, KeyEventArgs e)

            {

                if (e.KeyCode == Keys.D1 == true)

                {

                    IsZero = false;

                    if (CheckText == true)

                    {

                        CheckText = false;

                        txtInput.Text = "1";

                    }

                    else

                    {

                        txtInput.Text = txtInput.Text + 1;

                    }

                    if ((Add || Subtract || Multiply || Divide) == true)

                    {

                        button1 = true;

                    }

                }

            }

    Thats my code, could you tell me where I went wrong?

    Friday, July 3, 2009 6:12 PM
  • You don't need -> if (e.KeyCode == Keys.D1 == true)

    Just if (e.KeyCode == Keys.D1) will suffice.

    There doesn't seem to be anything else expicitly wrong with that code. Of course it doesn't seem to be complete since you don't handle the other keys. And I don't really know what you are doing with button1 there.


    http://blog.voidnish.com

    Friday, July 3, 2009 6:17 PM
  • Its not complete because I want to understand the concept before I do the rest of the keys. Also, what the code does is simply write the number "1" .
    Friday, July 3, 2009 6:22 PM
  • Ok, so are you getting any compilation errors here? Or does it not work as you expect it to when you run it?
    http://blog.voidnish.com
    Friday, July 3, 2009 6:25 PM
  • Im not getting any compilation errors, or warnings. Just that it never detects that the key has been pressed.
    Friday, July 3, 2009 6:28 PM
  • How did you setup the event handlers? Can you show that code too?
    http://blog.voidnish.com
    Friday, July 3, 2009 6:32 PM
  • I'm sorry, what event handlers?

    **I should've said before, I'm a novice in C#, and programming in general. The calculator has just been a project so I can learn.
    Friday, July 3, 2009 6:35 PM
  • You need to add a KeyDown event handler to your calculator form. Something like :

    calcForm.KeyDown += KeyDownHandler; <-- where that's the name of your event handler method
    http://blog.voidnish.com
    • Marked as answer by TheBlueCard Friday, July 3, 2009 7:04 PM
    Friday, July 3, 2009 6:54 PM
  • Thanks, I got it now.
    Friday, July 3, 2009 7:04 PM