Solved

Trapping KeyStrokes

Posted on 2004-04-10
9
334 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Exchange and Third Party application. 8 60
endX challenge 2 50
Magic Software info 18 105
Counting documents in a Domino View 3 36
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 …
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

743 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

9 Experts available now in Live!

Get 1:1 Help Now