How can I detect whenever DOWN ARROW Key is pressed?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dr Cake
    New Member
    • May 2012
    • 8

    How can I detect whenever DOWN ARROW Key is pressed?

    So, I've been working on a project recently, and the only thing remaining to do is a detector to whenever down arrow key is pressed.
    The down arrow key can only trigger when a certain check box is checked and this will have to work with a timer either.
    The problem is that I've got no idea how to make the down arrow key press detector(I've got the check box and timer ready tho). Glad if you can help. :)
  • Aimee Bailey
    Recognized Expert New Member
    • Apr 2010
    • 197

    #2
    Hi there, are you trying to capture this key event in WinForms or WPF?

    Comment

    • Dr Cake
      New Member
      • May 2012
      • 8

      #3
      Originally posted by Aimee Bailey
      Hi there, are you trying to capture this key event in WinForms or WPF?
      I'm trying to do this using Windows Forms.

      Comment

      • Aimee Bailey
        Recognized Expert New Member
        • Apr 2010
        • 197

        #4
        In WinForms there are various overload-able events that handle keyboard actions; OnKeyDown, OnKeyUp and OnKeyPressed. For this example i'll use OnKeyDown and OnKeyUp as OnKeyPressed only provides a char which does not include the arrow keys. Here is an example showing how you'd do it:

        Code:
        protected override void OnKeyDown(KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Down)
            {
                MessageBox.Show("Down key was pressed");
            }
        }
        
        protected override void OnKeyUp(KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Down)
            {
                MessageBox.Show("Down key was released");
            }
        }
        Something that is really important to note with these methods, is that they may not fire at all if you do not set the form's KeyPreview property to true.

        Hope this helps!

        Aimee Bailey.

        Comment

        • Dr Cake
          New Member
          • May 2012
          • 8

          #5
          Okay, thanks. But there is another problem now, I have 2 different methods, one for when the timer triggers, which calls the OnKeyDown method. However, when I use OnKeyDown() it says "No overload for method 'OnKeyDown' takes 0 arguments" and asks me to put an argument in OnKeyDown(), and this must be "KeyEventAr gs e" and I really don't know how to do that. Here's the code:
          Code:
          private void TimeInterval_Tick(object sender, EventArgs e)
          {
              OnKeyDown(); // this needs an argument.
          }
          
          protected override void OnKeyDown(KeyEventArgs e)
          {
              if (e.KeyCode == Keys.Down)
              {
                  // something
              }
          }

          Comment

          • Aimee Bailey
            Recognized Expert New Member
            • Apr 2010
            • 197

            #6
            The OnKeyDown method is called when a user presses a key on the keyboard, so I don't know why you'd want to automatically call that method through a timer. If you wanted to do something every time the timer ticked, then I'd just stick the code you wish to execute in place of line 3.

            Aimee.

            Comment

            • Dr Cake
              New Member
              • May 2012
              • 8

              #7
              Well, the code needs to work both when the timer ticks and while the down arrow key is pressed.

              Comment

              • PsychoCoder
                Recognized Expert Contributor
                • Jul 2010
                • 465

                #8
                Try this:

                Code:
                private void TimeInterval_Tick(object sender, EventArgs e)
                {
                    OnKeyDown(null, null); // this needs an argument.
                }
                 
                protected override void OnKeyDown(KeyEventArgs e)
                {
                    if (e.KeyCode == Keys.Down)
                    {
                        // something
                    }
                }

                Comment

                • Dr Cake
                  New Member
                  • May 2012
                  • 8

                  #9
                  I tried that new code. And when I built my program, I got this error: "Object reference not set to an instance of an object" on "if (e.KeyCode == Keys.Down)". And "OnKeyDown(null );" highlighted in grey. I believe null can't be an argument
                  Last edited by Dr Cake; Jun 5 '12, 05:00 PM. Reason: Editted to add some more details.

                  Comment

                  • Aimee Bailey
                    Recognized Expert New Member
                    • Apr 2010
                    • 197

                    #10
                    Usually when a method is marked as override, it means it is replacing a virtual method within the base class (or overriding it's original purpose). This means you really should not be calling OnKeyDown() at all, especially not calling it with null arguments.

                    Virtual methods allow the person who designed the (for this context 'Form') class to add extra functionality, which is why you can override them. If you wish a timer and the keyboard key to fire the same thing, then what you really need to do is this:

                    Code:
                    protected override void OnKeyDown(KeyEventArgs e)
                    {
                        if (e.KeyCode == Keys.Down)
                        {
                            DoWhatever();
                        }
                    }
                    
                    private void TimeInterval_Tick(object sender, EventArgs e)
                    {
                        DoWhatever();
                    }
                    
                    private void DoWhatever()
                    {
                        MessageBox.Show("Either down pressed, or timer ticked");
                    }
                    This may seem like more code, but it makes far more sense. For future reference even though it is possible to call overriden methods, it's best practice to avoid doing it.

                    Aimee.

                    Comment

                    • vaynenick
                      New Member
                      • Jun 2014
                      • 1

                      #11
                      protected override bool ProcessCmdKey(r ef Message msg, Keys keyData)
                      {
                      //handle your keys here
                      }
                      Full source : Arrow Keypress

                      Vayne

                      Comment

                      • gggustafson
                        New Member
                        • Jul 2017
                        • 1

                        #12
                        Although the use of an On... override is recommended, it is important to include within your handler an invocation of the base method such as

                        base.OnKeyDown ( e );

                        in the code for

                        protected override void OnKeyDown ( KeyEventArgs e )...

                        Comment

                        • lauryfriese
                          New Member
                          • Sep 2023
                          • 2

                          #13
                          To detect when the DOWN ARROW key is pressed in a C# application, you can handle the keyboard events of the form or control where you want to capture the key press. Here's an example:

                          ```csharp
                          using System;
                          using System.Windows. Forms;

                          public class MainForm : Form
                          {
                          public MainForm() Candy Crush
                          {
                          // Register the KeyDown event handler for the form
                          KeyDown += MainForm_KeyDow n;
                          }

                          private void MainForm_KeyDow n(object sender, KeyEventArgs e)
                          {
                          if (e.KeyCode == Keys.Down)
                          {
                          // DOWN ARROW key is pressed
                          Console.WriteLi ne("DOWN ARROW key pressed!");
                          }
                          }
                          }
                          ```

                          In this example, we handle the `KeyDown` event of the form (`MainForm`) and check if the pressed key's `KeyCode` matches `Keys.Down`. If it does, we print a message to the console indicating that the DOWN ARROW key was pressed.

                          Make sure to attach the event handler to the appropriate form or control in your application. If you're using a different framework or UI library, the event handling mechanism may differ slightly, but the general idea is the same: capture the `KeyDown` event and check the `KeyCode` property for the desired key.

                          Remember to focus the form or control so that it can receive keyboard events. You can do this by calling the `Focus()` method on the form or control instance.

                          Comment

                          Working...