Hei every body
First sorry about my English
I want to know how I can to Press on a key on a keyboard and its was like I am click on a button in a Form
Example:
If I click on a button in a Form he open MessegeBox and show hello
I want if i click ESCAPE in a keyborad its was like I click on a button
Thanks helpers
If you can show me the code because I'm new in c#
sorry about my English
question about button
Page 1 of 14 Replies - 949 Views - Last Post: 17 December 2012 - 10:29 AM
Replies To: question about button
#2
Re: question about button
Posted 11 December 2012 - 08:33 AM
To answer your question I'm going to assume that you are building a windows application with WinForms, since you didn't otherwise specify.
First you'll need to know upon what control the user's control focus will be set when you want to catch and handle the event. For instance, if you were building a chat program, then the control of interest would be the textbox where they are typing their text. On that control of interest you'll want to put a KeyPress event. This will be triggered every time any button is pressed on the keyboard. In the code for that event, you'll then want to test the event argument to determine which button was pressed and handle it accordingly.
Please note, the KeyPress event must be added to each and every control that could have focus. I've seen a lot of new coders that try to add the event to the form itself then wonder why it's not working. If you are typing in a textbox that is located on the form, the KeyPress event of the text box is triggered, not the KeyPress event of the form.
----NOTE----
My suggestion above will only catch the key by it's character code. It does not capture the key stroke by an enumeration.
First you'll need to know upon what control the user's control focus will be set when you want to catch and handle the event. For instance, if you were building a chat program, then the control of interest would be the textbox where they are typing their text. On that control of interest you'll want to put a KeyPress event. This will be triggered every time any button is pressed on the keyboard. In the code for that event, you'll then want to test the event argument to determine which button was pressed and handle it accordingly.
Please note, the KeyPress event must be added to each and every control that could have focus. I've seen a lot of new coders that try to add the event to the form itself then wonder why it's not working. If you are typing in a textbox that is located on the form, the KeyPress event of the text box is triggered, not the KeyPress event of the form.
private void Control_KeyPress(object sender, KeyPressEventArgs e)
{
if(e.KeyChar == 'q' || e.KeyChar == 'Q')
{
// Do something here when the q key is pressed
}
}
----NOTE----
My suggestion above will only catch the key by it's character code. It does not capture the key stroke by an enumeration.
This post has been edited by tlhIn`toq: 15 December 2012 - 07:10 PM
Reason for edit:: code tags added
#3
Re: question about button
Posted 15 December 2012 - 07:14 PM
Quote
Please note, the KeyPress event must be added to each and every control that could have focus.
No. Set the KeyPreview property of the form to true. Then use the same type of KeyPress event handler for the Form instead of for each control.
private void myChatForm_KeyPress(object sender, KeyPressEventArgs e)
{
if(e.KeyChar == 'q' || e.KeyChar == 'Q')
{
// Do something here when the q key is pressed
}
}
#4
Re: question about button
Posted 17 December 2012 - 10:05 AM
Hah! Years later and I'm still being schooled! Way to go tlhIn`toq!
I definitely like your solution and will keep it in mind the next time I need key controls on a form. Now just to figure out why I had never of that solution before...
I definitely like your solution and will keep it in mind the next time I need key controls on a form. Now just to figure out why I had never of that solution before...
#5
Re: question about button
Posted 17 December 2012 - 10:29 AM
We all run across different things based on need and experience. I'm only just now getting into coding for databases and consuming on-line services - Yet most 20 year olds do this in their sleep.
That's what I love about coding communities, we all learn from each other.
That's what I love about coding communities, we all learn from each other.
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote





|