473,405 Members | 2,379 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Reading keyboard key presses on WinForm

I have a WinForm with a single command button in the middle, I have
added a KeyDown event handler to the form and the method for this is:

private void Form2_KeyDown(object sender, KeyEventArgs e)
{
MessageBox.Show(e.KeyCode.ToString());
}

When I run the program and tap on the keyboard nothing happens, I set
a break-point and it is never reached. So I tried using the KeyPress
event instead and the same thing happens.

private void Form2_KeyPress(object sender, KeyPressEventArgs
e)
{
MessageBox.Show(e.KeyChar.ToString());
}

If I remove the button, both methods are called.... So it appears any
control placed on the page could have focus and prevent my general,
form-level keypress event handler from working as I would like. The
actual project I'm trying to incorporate this into could have many
controls on it, is there a general way to trap a key press by the user
regardless of which control on the form has focus, without adding a
keydown or keypress event handler for every control?

May 10 '07 #1
1 3809
Take a look at the form's KeyPreview property.

--
Brian Schwartz
FishNet Components
http://www.fishnetcomponents.com
Fish Grid .NET Light: Powerful Layouts for Small Datasets
"JDeats" <Je**********@gmail.comwrote in message
news:11*********************@u30g2000hsc.googlegro ups.com...
I have a WinForm with a single command button in the middle, I have
added a KeyDown event handler to the form and the method for this is:

private void Form2_KeyDown(object sender, KeyEventArgs e)
{
MessageBox.Show(e.KeyCode.ToString());
}

When I run the program and tap on the keyboard nothing happens, I set
a break-point and it is never reached. So I tried using the KeyPress
event instead and the same thing happens.

private void Form2_KeyPress(object sender, KeyPressEventArgs
e)
{
MessageBox.Show(e.KeyChar.ToString());
}

If I remove the button, both methods are called.... So it appears any
control placed on the page could have focus and prevent my general,
form-level keypress event handler from working as I would like. The
actual project I'm trying to incorporate this into could have many
controls on it, is there a general way to trap a key press by the user
regardless of which control on the form has focus, without adding a
keydown or keypress event handler for every control?

May 10 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: LRD | last post by:
Form not refreshing after autopostback unless keyboard or mouse move Hi, We created several new ASP.NET C# forms for our intranet. In each form we use panels for different sections of the...
40
by: googler | last post by:
I'm trying to read from an input text file and print it out. I can do this by reading each character, but I want to implement it in a more efficient way. So I thought my program should read one...
0
by: George Hartas | last post by:
I am using Visual C# .NET 2003 to make a ComboBox accept both mouse and keyboard selection. For mouse selection code, I double-clicked ComboBox to get the default "comboBox1_SelectedIndexChanged"...
5
by: nx-2000 | last post by:
I've got a very large C# forms app and now that its being used in bigger environments we're getting a steady stream of "why does it do this?" problems. The most nagging of which right now is that...
0
by: luc.saffre | last post by:
Hello, I thought that I should ask here for comments on a blog entry that I wrote some weeks ago. I am sure that other people have been thinking about this, but I didn't yet find them. The...
3
by: Moogoo | last post by:
Hello all, I am new to c++, coming from a Java background. Thank you in advance for anyone that can answer the question below: GOAL: I am currently writing a shell application for a...
11
by: vbguy2008 | last post by:
Hi, I am coding a Windows Form Application in VB.NET 2008. I would like to clear the keyboard buffer or at least empty all outstanding key presses queued up for my application at certain points...
2
by: Brice Rebsamen | last post by:
Hi I wrote the following program that reads the keyboard in medium raw mode (keycode mode). Here is the initialization code, the full code is at the end. fd = open("/dev/tty0", O_RDONLY);...
5
by: MikeB | last post by:
I have a simple form that I'm using to learn something about HTML and JavaScript. Can I associate a button on the form with a keyboard key so that when the user presses the keyboard key the form...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.