943,605 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 3105
  • C# RSS
Oct 19th, 2008
0

Problem with keyboard control in C#

Expand Post »
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:

C# Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 11
Solved Threads: 7
Posting Whiz in Training
Clawsy is offline Offline
225 posts
since Feb 2008
Oct 19th, 2008
0

Re: Problem with keyboard control in C#

What control currently has focus? Sometimes a control with focus can prevent some of these things working
Reputation Points: 196
Solved Threads: 190
Posting Virtuoso
LizR is offline Offline
1,735 posts
since Aug 2008
Oct 19th, 2008
0

Re: Problem with keyboard control in C#

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.
Reputation Points: 11
Solved Threads: 7
Posting Whiz in Training
Clawsy is offline Offline
225 posts
since Feb 2008
Oct 19th, 2008
0

Re: Problem with keyboard control in C#

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.
Reputation Points: 196
Solved Threads: 190
Posting Virtuoso
LizR is offline Offline
1,735 posts
since Aug 2008
Oct 19th, 2008
0

Re: Problem with keyboard control in C#

. LizR, you're right. The button ate my keys to access itself. THANKS! You solved my little annoying problem !
Reputation Points: 11
Solved Threads: 7
Posting Whiz in Training
Clawsy is offline Offline
225 posts
since Feb 2008
Oct 20th, 2008
0

Re: Problem with keyboard control in C#

Do mark the this thread as solved then
Reputation Points: 196
Solved Threads: 190
Posting Virtuoso
LizR is offline Offline
1,735 posts
since Aug 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: have a problem with inheritance
Next Thread in C# Forum Timeline: Create a login page for a URL





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC