Send Keys to Application
- Hi, I'm using a combination of the FindWindow and SetForegroundWindow APIs and the SendKeys() method to create an on screen keyboard with custom buttons for an application. The problem is when I'm using the program, the control which had focus before the foreground keyboard is clicked, no longer has focus and the keystroke does nothing. Is there a way to improve this? Please let me know.-Cameron
Cameron
Answers
- Consider adding the WS_EX_NOACTIVATE style to your form's window so it won't get focus when another window has focus and the user clicks on a button on your form:
const int WS_EX_NOACTIVATE = 0x8000000; protected override CreateParams CreateParams { get { CreateParams ret = base.CreateParams; ret.ExStyle |= WS_EX_NOACTIVATE; return ret; } }
- Marked As Answer byCameron Kloot Saturday, November 07, 2009 5:08 AM
- Hi,
Using WS_EX_NOACTIVATE as suggested is part of the solution, but is not enough on it's own. You also need to use 'window less' or non-focusable controls for the buttons, and you also need to ensure your form doesn't steal focus when it's first shown. I have a blog article about this here;
http://www.yortondotnet.com/2006/11/on-screen-keyboards.html
If you're working in WPF, I have tried it myself, but I have another article that has a WPF implementation of the same trick here;
http://www.yortondotnet.com/2009/09/on-screen-keyboards-in-wpf.html- Marked As Answer byCameron Kloot Saturday, November 07, 2009 5:08 AM
All Replies
- Consider adding the WS_EX_NOACTIVATE style to your form's window so it won't get focus when another window has focus and the user clicks on a button on your form:
const int WS_EX_NOACTIVATE = 0x8000000; protected override CreateParams CreateParams { get { CreateParams ret = base.CreateParams; ret.ExStyle |= WS_EX_NOACTIVATE; return ret; } }
- Marked As Answer byCameron Kloot Saturday, November 07, 2009 5:08 AM
- Hi,
Using WS_EX_NOACTIVATE as suggested is part of the solution, but is not enough on it's own. You also need to use 'window less' or non-focusable controls for the buttons, and you also need to ensure your form doesn't steal focus when it's first shown. I have a blog article about this here;
http://www.yortondotnet.com/2006/11/on-screen-keyboards.html
If you're working in WPF, I have tried it myself, but I have another article that has a WPF implementation of the same trick here;
http://www.yortondotnet.com/2009/09/on-screen-keyboards-in-wpf.html- Marked As Answer byCameron Kloot Saturday, November 07, 2009 5:08 AM
- Why would you need to use a windowless control? This simple app with normal buttons works without stealing focus. Is there some sort of interface issue I'm not seeing?
using System; using System.Collections.Generic; using System.Windows.Forms; using System.Drawing; namespace ExampleApp { class Program : Form { [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Program()); } TextBox m_textBox; Program() { int x = 0; int y = 0; foreach (string line in new string[] { "1234567890", "QWERTYUIOP", "ASDFGHJKL", "ZXCVBNM "}) { foreach (char cur in line) { Button button = new Button(); button.Location = new Point(x * 25, y * 25); button.Size = new Size(23, 23); button.Text = cur.ToString(); button.Click += new EventHandler(Button_Click); Controls.Add(button); x++; } x = 0; y++; } m_textBox = new TextBox(); m_textBox.Top = 25 * 4; m_textBox.Size = new Size(25 * 10, 23); Controls.Add(m_textBox); ClientSize = new Size(25 * 10, 25 * 5); TopMost = true; } void Button_Click(object sender, EventArgs e) { m_textBox.Text = ((Button)sender).Text; SendKeys.Send(m_textBox.Text); } const int WS_EX_NOACTIVATE = 0x8000000; protected override CreateParams CreateParams { get { CreateParams ret = base.CreateParams; ret.ExStyle |= WS_EX_NOACTIVATE; return ret; } } } }
- Thanks especially Yort, appreciate it.
Cameron - Hi,
I found that when I used normal buttons the button itself got focus, and then caused the parent to activate (even setting TabStop = false didn't help). It took me days to figure out that was what was happening, the form wouldn't get focus if clicked, but a button, text field etc. that was hosted on it would cause the active form to change and break the focus for the keyboard... using labels, picture boxes etc. that didn't get focus worked fine. Not sure why it works for you !! - No problem, glad to have helped :)


