View Single Post
Join Date: Feb 2008
Posts: 99
Reputation: Clawsy is an unknown quantity at this point 
Solved Threads: 5
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