Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 136,538 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,770 people online right now. Registration is fast and FREE... Join Now!




Capture Key Pressed

 
Reply to this topicStart new topic

Capture Key Pressed

Allen.Brumley
23 Aug, 2008 - 06:41 AM
Post #1

New D.I.C Head
*

Joined: 25 Jan, 2008
Posts: 16


My Contributions
I need to capture the key that is pressed on the key board and react accordingly. I've tried to figure out how to catch the key in KeyDown, KeyPress, and KeyUp using the event arg (e) with no luck.

CODE
private void myProg_KeyUp(Object sender, KeyEventArgs e)
{
switch (e.KeyValue)
case 0:
//todo work
break;
case 1:
//todo work
break;
}


this just isn't working and I'm not sure what I'm doing wrong. I'm new to programming and have not had to capture key press's before so this one has me stumped.

Any help would be appriciated. I need to capture the number keys, period key, enter key, and Symbol keys. I am ignoring the alphabet basically.

I was hoping that the basic calculator example would have this in it but I didn't find it there. (maybe it could be added?)

This post has been edited by Allen.Brumley: 23 Aug, 2008 - 06:43 AM
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Capture Key Pressed
23 Aug, 2008 - 07:38 AM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,213



Thanked: 217 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
I guess the question I have is "What control are you using to capture the events?"

If you start a new project, put on a textbox control, select it and add the keypress event you could do an event like this...

csharp

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
MessageBox.Show(e.KeyChar.ToString());
}


This would capture keys like letters, numbers, periods, symbols etc. Any of the character keys. It will then display them in a messagebox prior to adding them to the textbox. This allows you to evaluate them.

Hopefully this is what you are after. smile.gif
User is online!Profile CardPM
+Quote Post

Allen.Brumley
RE: Capture Key Pressed
23 Aug, 2008 - 12:50 PM
Post #3

New D.I.C Head
*

Joined: 25 Jan, 2008
Posts: 16


My Contributions
QUOTE(Martyr2 @ 23 Aug, 2008 - 10:38 AM) *

I guess the question I have is "What control are you using to capture the events?"

Hopefully this is what you are after. smile.gif


that was exactly what I was after. I'm catching the keypress from a text box. I just wasn't using the .ToString(() part which made it not work. Now I've got it as:

CODE

        private void MyProg_KeyPress(object sender, KeyPressEventArgs e)
        {
            switch (e.KeyChar.ToString())
            {
                case "0":
                    kryBtn0_Click(sender, e);
                    e.Handled = true;
                    break;
                case "1":
                    kryBtn1_Click(sender, e);
                    e.Handled = true;
                    break;
                case "2":
                    kryBtn2_Click(sender, e);
                    e.Handled = true;
                    break;
                 default:
                     e.Handled = true;
                     break;
             }
          }


Made all the differance in the world, Thanks!

This post has been edited by Allen.Brumley: 23 Aug, 2008 - 01:00 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 10:25PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month