Sign in to follow this  
benryves

[.net] Capturing keyboard focus

Recommended Posts

I have a multiple document interface, and a number of child forms. One of these forms is just a plain form with no controls on it, which I need to capture keyboard focus with (it's the video output form for an emulator - the BackgroundImage is set to the current image). The problem is, if I have one of the other forms open (for example, the memory viewer window with a vertical scrollbar) and press a cursor key (again, for example) that form is brought into focus as it's scrollbar steals focus. My understanding is that certain controls somehow indicate that they accept keyboard focus - how would you do this? I want my control-free display form to capture keyboard input. Currently I've put a text box on it, but hidden it 30 pixels or so above the top of the form, and this works fine, but is horrible.

Share this post


Link to post
Share on other sites
This is C#. I've gone up and down through the properties window for the form, looking for something like a boolean AcceptsKeyboardFocus, to no avail.

Share this post


Link to post
Share on other sites
Set the KeyPreview property for the form and do something like:


private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
MessageBox.Show("You pressed" + e.KeyChar);
}

Share this post


Link to post
Share on other sites
Interesting, not really played with MDI C# projects.

Does your control-less form get other buttons properly? Is it just cursor keys?
If that's a case it might be that you need to override the ProcessCmdKey method either in that form. Cursor keys, control+tab and all that probably come under Command keys rather then normal keys.

Share this post


Link to post
Share on other sites
Quote:
Original post by paulecoyote
Does your control-less form get other buttons properly? Is it just cursor keys?
If that's a case it might be that you need to override the ProcessCmdKey method either in that form. Cursor keys, control+tab and all that probably come under Command keys rather then normal keys.
That certainly sounds more plausible as yes, it handles most other keys just fine.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this