Skip to main content
GameDev.net gamedev.net
🔒 Locked

[.net] Capturing keyboard focus

Started by benryves May 22, 2006 at 5:39 AM 4 replies 2.6k views
Original Post
benryves
benryves
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.
[Website] [+++ Divide By Cucumber Error. Please Reinstall Universe And Reboot +++]
paulecoyote
paulecoyote
Hi Ben, which language you doing this in? It might be just a case of setting the control style.
Anything posted is personal opinion which does not in anyway reflect or represent my employer. Any code and opinion is expressed “as is” and used at your own risk – it does not constitute a legal relationship of any kind.
benryves
benryves
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.
[Website] [+++ Divide By Cucumber Error. Please Reinstall Universe And Reboot +++]
Machaira
Machaira
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);}
Former Microsoft XNA and Xbox MVP | Check out my blog for random ramblings on game development
paulecoyote
paulecoyote
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.
Anything posted is personal opinion which does not in anyway reflect or represent my employer. Any code and opinion is expressed “as is” and used at your own risk – it does not constitute a legal relationship of any kind.
benryves
benryves
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.
[Website] [+++ Divide By Cucumber Error. Please Reinstall Universe And Reboot +++]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.