473,320 Members | 2,111 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How can I detect whenever DOWN ARROW Key is pressed?

Dr Cake
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. :)
Jun 3 '12 #1
12 42383
Aimee Bailey
197 Expert 100+
Hi there, are you trying to capture this key event in WinForms or WPF?
Jun 4 '12 #2
@Aimee Bailey
I'm trying to do this using Windows Forms.
Jun 4 '12 #3
Aimee Bailey
197 Expert 100+
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:

Expand|Select|Wrap|Line Numbers
  1. protected override void OnKeyDown(KeyEventArgs e)
  2. {
  3.     if (e.KeyCode == Keys.Down)
  4.     {
  5.         MessageBox.Show("Down key was pressed");
  6.     }
  7. }
  8.  
  9. protected override void OnKeyUp(KeyEventArgs e)
  10. {
  11.     if (e.KeyCode == Keys.Down)
  12.     {
  13.         MessageBox.Show("Down key was released");
  14.     }
  15. }
  16.  
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.
Jun 4 '12 #4
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 "KeyEventArgs e" and I really don't know how to do that. Here's the code:
Expand|Select|Wrap|Line Numbers
  1. private void TimeInterval_Tick(object sender, EventArgs e)
  2. {
  3.     OnKeyDown(); // this needs an argument.
  4. }
  5.  
  6. protected override void OnKeyDown(KeyEventArgs e)
  7. {
  8.     if (e.KeyCode == Keys.Down)
  9.     {
  10.         // something
  11.     }
  12. }
Jun 5 '12 #5
Aimee Bailey
197 Expert 100+
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.
Jun 5 '12 #6
Well, the code needs to work both when the timer ticks and while the down arrow key is pressed.
Jun 5 '12 #7
PsychoCoder
465 Expert Mod 256MB
Try this:

Expand|Select|Wrap|Line Numbers
  1. private void TimeInterval_Tick(object sender, EventArgs e)
  2. {
  3.     OnKeyDown(null, null); // this needs an argument.
  4. }
  5.  
  6. protected override void OnKeyDown(KeyEventArgs e)
  7. {
  8.     if (e.KeyCode == Keys.Down)
  9.     {
  10.         // something
  11.     }
  12. }
Jun 5 '12 #8
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
Jun 5 '12 #9
Aimee Bailey
197 Expert 100+
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:

Expand|Select|Wrap|Line Numbers
  1. protected override void OnKeyDown(KeyEventArgs e)
  2. {
  3.     if (e.KeyCode == Keys.Down)
  4.     {
  5.         DoWhatever();
  6.     }
  7. }
  8.  
  9. private void TimeInterval_Tick(object sender, EventArgs e)
  10. {
  11.     DoWhatever();
  12. }
  13.  
  14. private void DoWhatever()
  15. {
  16.     MessageBox.Show("Either down pressed, or timer ticked");
  17. }
  18.  
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.
Jun 5 '12 #10
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
//handle your keys here
}
Full source : Arrow Keypress

Vayne
Jun 19 '14 #11
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 )...
Jul 21 '17 #12
lauryfriese
2 Bit
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_KeyDown;
}

private void MainForm_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Down)
{
// DOWN ARROW key is pressed
Console.WriteLine("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.
Sep 14 '23 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Bob Alston | last post by:
Anyone know how to hide the down arrow that is at the far right of a combo box? I have seen it done (with auto display drow-down, of course). Bob
2
by: VB Programmer | last post by:
I have a text box. If the user hits the down arrow while in the text box I want to process some code. In the KeyPress event of the text box how do I capture the down arrow (I assume Keys.Down)?...
2
by: foo | last post by:
Hello, I can trap alphanumeric keys wih this code but the event doesn't even fire when a page down, page up, home, end is pressed. I've overloaded the OnKeyPress event for the form. The event...
5
by: Ivan V via DotNetMonster.com | last post by:
Hi I would like to knwo if I could activate the up and down arrow in a window form which have the same function as using the tab button. Thanks for all! best rgds, Ivan Vong -- Message...
4
by: Brian Henry | last post by:
Using controlpaint i can draw an arrorw pointing to the right like this ControlPaint.DrawMenuGlyph(e.Graphics, New Rectangle(0, 0, e.Item.Bounds.Width, e.Item.Bounds.Height), MenuGlyph.Arrow) ...
0
by: Andrus | last post by:
I have combobox column in DataGridView. Up and down error keys should be used to navigate previous and next row in grid. For this I override them in ProcessCmdKey() event. When combobox...
1
by: Jenny | last post by:
Dear all, I have one select which lists one visible element. I want this select can show scrollbar after I click its down arrow, so that i can use the scrollbar to select the element i want. ...
0
by: cmrhema | last post by:
Hello I am using ASP.net 2.0 with VC# 2005. I have populated a listbox as below and when I click on the value on the list box I display out the related values. All works fine. But I have one...
0
by: kronus | last post by:
Hi everyone, Yes, I need to use you guys as a sounding board once more :-) I have a button that has two listeners one for mouse up and the other for mouse down and they point to the same...
14
by: DanicaDear | last post by:
I have a QTY field and an ITEM field. User inputs a number into the QTY field, then tabs over the the next field and there's a down arrow on the right hand side of the field that you click to choose...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.