提出问题提出问题
 

已答复Send Keys to Application

  • 2009年11月6日 2:02Cameron Kloot 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    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

答案

全部回复

  • 2009年11月6日 3:29An Anon Coward 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     已答复包含代码
    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;
        }
    }
    
    
  • 2009年11月6日 3:46Yort 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     已答复
    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
  • 2009年11月6日 5:29An Anon Coward 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     包含代码
    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;
                }
            }
        }
    }
    
    
  • 2009年11月7日 5:08Cameron Kloot 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    Thanks especially Yort, appreciate it.  

    Cameron
  • 2009年11月7日 20:25Yort 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    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 !!

  • 2009年11月7日 20:25Yort 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    No problem, glad to have helped :)