RSS Forums RSS

Problem with keyboard control in C#

Please support our C# advertiser: DiscountASP.NET – 3 Months Free on C# Web Hosting
Reply
Posts: 47
Reputation: Clawsy is an unknown quantity at this point 
Solved Threads: 2
Clawsy Clawsy is offline Offline
Light Poster

Problem with keyboard control in C#

  #1  
Oct 19th, 2008
Hello,
Recently I needed to assign keyboard control to my program; so and I searched the Internet, I found many examples but it seems all have one little problem(or it's my mistake?):
I use this test code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace myheys
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

        }



        private void Form1_Load(object sender, EventArgs e)
        {
            this.KeyUp += new KeyEventHandler(button1_KeyDown);
        }



        private void button1_KeyDown(object sender, KeyEventArgs e)
        {
            switch ((int)e.KeyValue)
            {
                case (int)Keys.Space: MessageBox.Show("SPACE"); break;
                case (int)Keys.Escape: MessageBox.Show("ESC"); break;
                case (int)Keys.Enter: MessageBox.Show("ENTER"); break;
                case (int)Keys.Delete: MessageBox.Show("del"); break;

                case (int)Keys.Up: MessageBox.Show("up"); break;
                case (int)Keys.Down: MessageBox.Show("down"); break;

                case (int)Keys.Left: MessageBox.Show("left"); break;
                case (int)Keys.Right: MessageBox.Show("right"); break;

                case (int)Keys.F10: MessageBox.Show("F10"); break;

                case (int)Keys.F: MessageBox.Show("f"); break;

            }
        }
    }
}

Keyboard control works fine but not for all key: Enter, Up, Down, Right, Left -> these are not working. . I don't understand why, because these controls are defined in c# like the others. If you have an idea, please post it. Thanks
AddThis Social Bookmark Button
Reply With Quote  
Posts: 1,734
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 184
LizR LizR is offline Offline
Posting Virtuoso

Re: Problem with keyboard control in C#

  #2  
Oct 19th, 2008
What control currently has focus? Sometimes a control with focus can prevent some of these things working
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
Reply With Quote  
Posts: 47
Reputation: Clawsy is an unknown quantity at this point 
Solved Threads: 2
Clawsy Clawsy is offline Offline
Light Poster

Re: Problem with keyboard control in C#

  #3  
Oct 19th, 2008
Thanks for reply. It has assigned 'button1_KeyDown' to the keydown property. 'button1' is focused. other keys work but those keys dont work.
Last edited by Clawsy : Oct 19th, 2008 at 9:56 am.
Reply With Quote  
Posts: 1,734
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 184
LizR LizR is offline Offline
Posting Virtuoso

Re: Problem with keyboard control in C#

  #4  
Oct 19th, 2008
The button is eating your keys, if you remove the button the problem goes too If all you're using is the button to get the events setup, dont, use the forms direct.
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
Reply With Quote  
Posts: 47
Reputation: Clawsy is an unknown quantity at this point 
Solved Threads: 2
Clawsy Clawsy is offline Offline
Light Poster

Re: Problem with keyboard control in C#

  #5  
Oct 19th, 2008
. LizR, you're right. The button ate my keys to access itself. THANKS! You solved my little annoying problem !
Reply With Quote  
Posts: 1,734
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 184
LizR LizR is offline Offline
Posting Virtuoso

Re: Problem with keyboard control in C#

  #6  
Oct 20th, 2008
Do mark the this thread as solved then
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Similar Threads
Other Threads in the C# Forum
Views: 1246 | Replies: 5 | Currently Viewing: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 2:31 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC