Solved

Trapping KeyStrokes

Posted on 2004-04-10
9
341 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
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…

789 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