Solved

Trapping KeyStrokes

Posted on 2004-04-10
9
336 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
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 12

Accepted Solution

by:
farsight earned 125 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

920 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now