Solved

Trapping KeyStrokes

Posted on 2004-04-10
9
343 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ejb stateless example 2 63
Object Oriented Programming, C#, referencing, scoping. 13 99
How do I fix this situation in GIT? 2 71
CSV file copy field 1 to field 2 2 58
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

738 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