Problem with keyboard control in C#

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2008
Posts: 83
Reputation: Clawsy is an unknown quantity at this point 
Solved Threads: 4
Clawsy Clawsy is offline Offline
Junior Poster in Training

Problem with keyboard control in C#

 
0
  #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:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace myheys
  11. {
  12. public partial class Form1 : Form
  13. {
  14. public Form1()
  15. {
  16. InitializeComponent();
  17.  
  18. }
  19.  
  20.  
  21.  
  22. private void Form1_Load(object sender, EventArgs e)
  23. {
  24. this.KeyUp += new KeyEventHandler(button1_KeyDown);
  25. }
  26.  
  27.  
  28.  
  29. private void button1_KeyDown(object sender, KeyEventArgs e)
  30. {
  31. switch ((int)e.KeyValue)
  32. {
  33. case (int)Keys.Space: MessageBox.Show("SPACE"); break;
  34. case (int)Keys.Escape: MessageBox.Show("ESC"); break;
  35. case (int)Keys.Enter: MessageBox.Show("ENTER"); break;
  36. case (int)Keys.Delete: MessageBox.Show("del"); break;
  37.  
  38. case (int)Keys.Up: MessageBox.Show("up"); break;
  39. case (int)Keys.Down: MessageBox.Show("down"); break;
  40.  
  41. case (int)Keys.Left: MessageBox.Show("left"); break;
  42. case (int)Keys.Right: MessageBox.Show("right"); break;
  43.  
  44. case (int)Keys.F10: MessageBox.Show("F10"); break;
  45.  
  46. case (int)Keys.F: MessageBox.Show("f"); break;
  47.  
  48. }
  49. }
  50. }
  51. }

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
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: Problem with keyboard control in C#

 
0
  #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 Quick reply to this message  
Join Date: Feb 2008
Posts: 83
Reputation: Clawsy is an unknown quantity at this point 
Solved Threads: 4
Clawsy Clawsy is offline Offline
Junior Poster in Training

Re: Problem with keyboard control in C#

 
0
  #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 10:56 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: Problem with keyboard control in C#

 
0
  #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 Quick reply to this message  
Join Date: Feb 2008
Posts: 83
Reputation: Clawsy is an unknown quantity at this point 
Solved Threads: 4
Clawsy Clawsy is offline Offline
Junior Poster in Training

Re: Problem with keyboard control in C#

 
0
  #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 Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: Problem with keyboard control in C#

 
0
  #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 Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C# Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC