Visual Studio Development Bookmark and Share   
 index > Visual C# Express Edition > How can i find out witch keyboard button the user has pressed?
 

How can i find out witch keyboard button the user has pressed?

the idea is i will display at random a picture of a number or letter and the user ( my 3 year old ) will hit the relavant key on the keyboard.

but i have no idea how to find out what key has been pressed i have been searching the help and here but not realy sure what to look for.
any help would be greatly appreceated....
i was hoping for a solution that wouldnt require a text box as that would require that she allways has the textbox in focus plus all i wanted on the screen was a picbox.
DARREN HANSON  Thursday, October 15, 2009 5:54 PM
You can have your main Form (which inherits from Control) override OnKeyDown .




Reed Copsey, Jr. - http://reedcopsey.com


Take a look at the link.
Follow Reed's advice.
It is a better practice to override the OnEvent methods in class than actually subscribing to the events themselves.

Simply override the method. Guess what? The IDE will do most of the work for you.
Type the keyword "override" then press the "SpaceBar". The IDE will provide a list of all virtualmethods defined within the class.
Simply scroll down to the OnKeyDown method and press the "TAB" key.
The IDE generates the signature for you, including the call to the base class method.
Generate a message box by getting the KeyCode form the EventArgs parameter in the method signature.

Rudedog =8^D
Mark the best replies as answers. "Fooling computers since 1971."
Rudedog2  Sunday, October 18, 2009 5:05 PM
You can have your main Form (which inherits from Control) override OnKeyDown .

This will run any time the user (your 3 year old) presses a key.  In this method, you can see if it's the right key, and if so, do whatever it is you're going to do to make him realize he was right.



Reed Copsey, Jr. - http://reedcopsey.com
  • Proposed As Answer byRudedog2 Sunday, October 18, 2009 5:05 PM
  •  
Reed Copsey, Jr.  Thursday, October 15, 2009 6:05 PM
i was hoping for a solution that wouldnt require a text box as that would require that she allways has the textbox in focus plus all i wanted on the screen was a picbox.


I am very new to programing so please excuse my lack of knowledge.
DARREN HANSON  Thursday, October 15, 2009 7:49 PM
No - you don't need a textbox.  Any control, including a form or picturebox, can implement this.

Just make a form that's full screen, and put your PictureBox in the form (with Dock set to Fill, so it fills the form).

Subscribe to the PictureBox.KeyDown event, and voila, you're there.

Reed Copsey, Jr. - http://reedcopsey.com
Reed Copsey, Jr.  Thursday, October 15, 2009 7:53 PM
If you set the Form's KeyPreview property to true, you can determine what key was pressed in the PreviewKeyDown event no matter what control has the focus. 
JohnWein  Thursday, October 15, 2009 9:50 PM
COULD YOU EXPLAIN IN MORE DETAIL PLEASE?

I am very new to programing so please excuse my lack of knowledge.
DARREN HANSON  Saturday, October 17, 2009 11:16 PM
You can have your main Form (which inherits from Control) override OnKeyDown .




Reed Copsey, Jr. - http://reedcopsey.com


Take a look at the link.
Follow Reed's advice.
It is a better practice to override the OnEvent methods in class than actually subscribing to the events themselves.

Simply override the method. Guess what? The IDE will do most of the work for you.
Type the keyword "override" then press the "SpaceBar". The IDE will provide a list of all virtualmethods defined within the class.
Simply scroll down to the OnKeyDown method and press the "TAB" key.
The IDE generates the signature for you, including the call to the base class method.
Generate a message box by getting the KeyCode form the EventArgs parameter in the method signature.

Rudedog =8^D
Mark the best replies as answers. "Fooling computers since 1971."
Rudedog2  Sunday, October 18, 2009 5:05 PM
It is a better practice to override the OnEvent methods in class than actually subscribing to the events themselves.


For seasoned knowledgable programmers who understand the difference. When advising novices on these forums, if the task can be accomplished by subscribing to the event, that is the better practice as it will lead to fewer errors.
JohnWein  Sunday, October 18, 2009 5:58 PM
ive got it working when a key is pressed the messagebox is displayed.
but how do i find out witch key was pressed?


I am very new to programing so please excuse my lack of knowledge.
DARREN HANSON  Sunday, October 18, 2009 7:45 PM
Implement the last line in Rudedog's post.
JohnWein  Sunday, October 18, 2009 8:04 PM
i used e.keyvalue witch gives me the int value of the key.

is there a list of the codes and the actual key name ie 13 = enter?

I am very new to programing so please excuse my lack of knowledge.
DARREN HANSON  Sunday, October 18, 2009 8:43 PM

Try some of the other properties of the EventArgs parameter. One of them is an enum, type Keys.


Mark the best replies as answers. "Fooling computers since 1971."
Rudedog2  Sunday, October 18, 2009 8:50 PM
i cant find type keys

i have looked at the locals when debuging all i can find is the value

but i have made my own list now so all is good



thank you all for your help.


I am very new to programing so please excuse my lack of knowledge.
DARREN HANSON  Sunday, October 18, 2009 9:04 PM
Nahp! My bad.
Trying to get you be developer detective on your own.

Use this ...

string typedKey = e.KeyCode.ToString();

Look use the arrow keys to move the selection from the IDE to each property, and you will see the type as it was declared in the source file.

Rudy =8^D
Mark the best replies as answers. "Fooling computers since 1971."
Rudedog2  Sunday, October 18, 2009 9:24 PM
It is a better practice to override the OnEvent methods in class than actually subscribing to the events themselves.


For seasoned knowledgable programmers who understand the difference. When advising novices on these forums, if the task can be accomplished by subscribing to the event, that is the better practice as it will lead to fewer errors.

I agree, John.

But, I think Darren has the right stuff.
Mark the best replies as answers. "Fooling computers since 1971."
Rudedog2  Sunday, October 18, 2009 9:25 PM
I'm not big on embellishing my responses. If the event is adequate, I don't have to explain the call to the base and where it should be positioned.
JohnWein  Sunday, October 18, 2009 9:36 PM
call me simple if you like but i have know idea what you mean sorry.

I am very new to programing so please excuse my lack of knowledge.
DARREN HANSON  Monday, October 19, 2009 2:33 PM
call me simple if you like but i have know idea what you mean sorry.

I am very new to programing so please excuse my lack of knowledge.

Sorry for using your thread for discussion unrelated to your post.
JohnWein  Monday, October 19, 2009 2:37 PM
all well and good rudy but that just gives me a string version of the code ie
72 = "72" but have got my head around that now anyway and have a new problem heres the link if you fancy helping me out yet again. lol

http://social.msdn.microsoft.com/Forums/en-US/Vsexpressvcs/thread/f5207db0-a94f-4600-9d96-68bec6e11f18

I am very new to programing so please excuse my lack of knowledge.
DARREN HANSON  Monday, October 19, 2009 2:37 PM
You should get the enum text value, not the integer equivalent.
This method should do approximately the same thing.
You would need to open the IDE's Output Window to see the results of keypresses.
I also included code that demonstrates key combinations, like CTRL-S.


protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
' The value of msg.MSG really should be 256 everytime this method is called. Better safe than sorry.
if (msg.Msg == 256) 'this line detects if the current message, msg, sent to the window is related to a keypress event.
{
Debug.WriteLine("The pressed key was " + keyData.ToString()); ' this line reports any keypress to Output Window
if (keyData == (Keys.Control | Keys.S))
{
MessageBox.Show("CTRL-S was pressed! ");
}
}
return base.ProcessCmdKey(ref msg, keyData); 'this is the required call to the base class to keep window running properly.
}
Mark the best replies as answers. "Fooling computers since 1971."
Rudedog2  Monday, October 19, 2009 2:47 PM

You can use google to search for other answers

Custom Search

More Threads

• Disregarding CASE in a string
• Timer
• Adding Data to Specific DGV Column
• DataGridView Not Showing Data
• Component is grey in toolbox
• I need Help Getting Started lol i Need it Badly
• Modding the Pong starter kit - questions
• How do you delete rows from a dataGridView based on a cell?
• how to change diacritics color in my arabic text ?
• x,y position of e.graphics???