Solved

Visual C# - How to Capture keypress for Enter, Up, Down, Left, Right

Posted on 2008-10-29
4
5,425 Views
Last Modified: 2013-12-17
ARG !

This is driving me nutz!

I am converting one of my VB6 apps to C# (I'm pretty new to c#). I currently have this working great in VB6, but I can't seem to find how to do it in c#, I've searched the net and my 8 c# books for hours and can't figure out how to get the job done.

Here is what I need:

Need list:
I need to be able to capture keyboard events for the Up, Down, Left, & Right arrows along with the enter key in my c# application.

Want list:
I would like to be able to turn the key capture on and off.

Reason:
What this app is, is a troubleshooter of sorts, where I need the ability to test for up, down, left, right, and enter, but that is only a part of the test. After the results are in from that, I don't want it to keep detecting those motions.

I would REALLY appreciate some help here guys.

Thanks!
0
Comment
Question by:MiSheps
  • 2
  • 2
4 Comments
 

Author Comment

by:MiSheps
ID: 22837024
ok,... i was playing around with some bits of code I found, and I think I'm almost there, but now I need to know how to turn it off. specifically the enter key.

So, here is the code I'm using now, but how to I temporarily disable the enter key press?


            protected override bool ProcessKeyPreview(ref System.Windows.Forms.Message m)

            {

                switch (m.WParam.ToInt32())

                {

                    case 37: // <--- left arrow.

                        if (MyGlobalVariables.JoystickTestRunning == true)

                        {

                            MessageBox.Show("Left");

                        }

                        break;

                    case 38: // <--- up arrow.

                        if (MyGlobalVariables.JoystickTestRunning == true)

                        {

                            MessageBox.Show("Up");

                        }

                        break;

                    case 39: // <--- right arrow.

                        if (MyGlobalVariables.JoystickTestRunning == true)

                        {

                            MessageBox.Show("Right");

                        }

                        break;

                    case 40: // <--- down arrow.

                        if (MyGlobalVariables.JoystickTestRunning == true)

                        {

                            MessageBox.Show("Down");

                        }

                        break;

                    case 13: // <--- Enter Key

                        if (MyGlobalVariables.JoystickTestRunning == true)

                        {

                            MessageBox.Show("Enter");

                        }

                        break;

                }

                return false;

            }

Open in new window

0
 
LVL 2

Expert Comment

by:Clausewitz
ID: 22840188
case 13: // <--- Enter Key
                        if (MyGlobalVariables.JoystickTestRunning == true)
                        {
                            // MessageBox.Show("Enter");
                            return true;
                        }
                        break;

If you return true, the key event is marked as processed and so the other keyboard events should not be triggered.
0
 
LVL 2

Accepted Solution

by:
Clausewitz earned 250 total points
ID: 22840261
0
 

Author Comment

by:MiSheps
ID: 22843279
ClauseWitz:

I tried your suggestion, and it didn't work, do I have something wrong?

                        case 13: // <--- Enter Key

                            if (MyGlobalVariables.JoystickTestRunning == true)

                            {

                                if (this.lblJoyStickAction2.Text == "BUTTON!")

                                {

                                    this.lblJoystickBUTTON.ForeColor = System.Drawing.Color.Black;

                                    this.lblJoystickBUTTON.BackColor = System.Drawing.Color.LightGreen;

                                    this.lblJoystickBUTTON.Text = "Joystick BUTTON Passed!";

                                    MyGlobalVariables.EndResultJoystickBUTTON = "Joystick BUTTON Passed!";

                                    MyGlobalVariables.JoyStickBUTTONFailed = false;
 

                                    this.timer1.Enabled = false;

                                    this.btnRunIndividualTests3.Visible = true;

                                    this.btnRunIndividualTests3.BringToFront();
 

                                    this.lblJoyStickAction1.Text = "Move Joystick";

                                    this.lblJoyStickAction2.Text = "UP!";

                                    this.lblJoyStickAction1.Visible = false;

                                    this.lblJoyStickAction2.Visible = false;

                                    this.btnJoystickNotWork.Visible = false;

                                    this.lblJoystick.Visible = false;

                                    this.btnShutdown.Visible = true;

                                    this.lblCountdownTime.Text = MyGlobalVariables.JoyStickWaitTimer;

                                    this.DisplayJoystickResults();

                                    return true;

                                }

                            }

                            break;

Open in new window

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

Suggested Solutions

Title # Comments Views Activity
Windows Service to Receive TCP Packets 4 124
Error in page 3 46
How to add <appSettings> to the webconfig file 2 21
Visual Studio 2013 and Eclipse Neon 6 8
A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

862 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

24 Experts available now in Live!

Get 1:1 Help Now