?
Solved

Trapping KeyStrokes

Posted on 2004-04-10
9
Medium Priority
?
345 Views
Last Modified: 2012-06-21
I'm trying to trap keystrokes in my C# program and it doesn't seem to be working, can someone tell me what I'm doing wrong? Here is My Code:

private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
            {
                  e.Handled = true;
                  switch(e.KeyCode)
                  {
                        case Keys.Left:
                              lbl1.Text = "Left";
                              break;
                        case Keys.Up:
                              lbl1.Text = "Up";
                              break;
                        case Keys.Right:
                              lbl1.Text = "Right";
                              break;
                        case Keys.Down:
                              lbl1.Text = "Down";
                              break;
                        default:
                              e.Handled = true;
                              break;
                  }
            }
0
Comment
Question by:Miatchguy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 10797670
Set KeyPreview form property to true.
0
 

Author Comment

by:Miatchguy
ID: 10799696
I did that, but it still doesn't work. The trouble I'm having is in the:
private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
part. I put a message box before the case switch and nothing happens when I press a key. Can someone tell me what to do or show me an example of some workning code?
0
 

Expert Comment

by:AruneshGupta
ID: 10800264

I used the notion of making a key sequence a HotKey

And using the Windows Message Dispatch loop to catch it


It was quick, and easy, and thje code is available in the site!
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 12

Accepted Solution

by:
farsight earned 500 total points
ID: 10800569

In VB.NET, you need the "Handles" clause on the event handler routine declaration.  The NAME of the function doesn't even matter in .NET.

    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown

In C#, the equivalent is code to add the eventhandler, either automatically added by the VS.NET designer in the InitializeComponent routine, or inserted in code by the programmer.
  this.KeyDown += new System.Windows.Forms.KeyEventHandler (this.Form1_KeyDown);


private void Form1_Load(object sender, System.EventArgs e)
      {
            this.KeyDown += new System.Windows.Forms.KeyEventHandler (this.Form1_KeyDown);
      }

private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
      {
            System.Diagnostics.Debug.WriteLine("in Form1_KeyDown");
      }
0
 
LVL 2

Expert Comment

by:mahanatti
ID: 10801466
Hi,

Place the statement e.Handled = true after the switch block.
Hope this helps.

-Maha.
0
 
LVL 12

Expert Comment

by:farsight
ID: 10978757
I actually tried my solution in code before posting it, if that's worth anything.
It worked fine for me.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Simple Linear Regression
Starting up a Project

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question