发送信件';i';使用SendKeys

本文关键字:使用 SendKeys | 更新日期: 2023-09-27 18:21:24

我用c#Windows窗体制作了一个屏幕键盘。我使用Sendkeys.Send()函数来发送按键。除了字母i以外的所有字母都可以正常工作。当Microsoft Word打开时,当我按下键盘上的字母I时,它会发送Ctrl+1I

在我的代码中,我用SendKeys.Send(value);发送按键,其中value是按下的按钮的文本。我得到了以下代码的文本:

string s = ((Button)sender).Text;

关于它为什么不能正常工作,有什么意见吗?

编辑:我用一个按钮创建了一个新的windows窗体项目,整个代码如下。仍然不起作用。任何想法都将不胜感激。

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            SendKeys.Send("i");
        }
        // Prevent form being focused
        const int WS_EX_NOACTIVATE = 0x8000000;
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams ret = base.CreateParams;
                ret.ExStyle |= WS_EX_NOACTIVATE;
                return ret;
            }
        }  
    }

被覆盖的函数用于防止窗体被聚焦。这样我就可以将击键发送到具有焦点的其他应用程序。

发送信件';i';使用SendKeys

两种选择:

1-模拟按键,请参见http://msdn2.microsoft.com/en-us/library/system.windows.forms.sendkeys(VS.71).aspx

样品:

public static void ManagedSendKeys(string keys)
        {
            SendKeys.SendWait(keys);
            SendKeys.Flush();
        }

2-向窗口发送一个键,按下按钮x秒

[DllImport("user32.dll")]
public static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, UIntPtr dwExtraInfo);
public static void KeyboardEvent(Keys key, IntPtr windowHandler, int delay)
        {
            const int KEYEVENTF_EXTENDEDKEY = 0x1;
            const int KEYEVENTF_KEYUP = 0x2;
            keybd_event((byte)key, 0x45, KEYEVENTF_EXTENDEDKEY, (UIntPtr)0);
            Thread.Sleep(delay);
            keybd_event((byte)key, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, (UIntPtr)0);
        }

您没有调用"SetForegroundWindow"Win32 API方法。因此,您的"SendKeys"调用可能会将密钥发送到您的应用程序,而不是预期的目标应用程序。

下面是MSDN上的一个例子:

如何:在代码中模拟鼠标和键盘事件

此外,以下是示例中的代码:

using System;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Windows.Forms;
namespace SimulateKeyPress
{
    class Form1 : Form
    {
        private Button button1 = new Button();
        [STAThread]
        public static void Main()
        {
            Application.EnableVisualStyles();
            Application.Run(new Form1());
        }
        public Form1()
        {
            button1.Location = new Point(10, 10);
            button1.TabIndex = 0;
            button1.Text = "Click to automate Calculator";
            button1.AutoSize = true;
            button1.Click += new EventHandler(button1_Click);
            this.DoubleClick += new EventHandler(Form1_DoubleClick);
            this.Controls.Add(button1);
        }
        // Get a handle to an application window.
        [DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
        public static extern IntPtr FindWindow(string lpClassName,
            string lpWindowName);
        // Activate an application window.
        [DllImport("USER32.DLL")]
        public static extern bool SetForegroundWindow(IntPtr hWnd);
        // Send a series of key presses to the Calculator application.
        private void button1_Click(object sender, EventArgs e)
        {
            // Get a handle to the Calculator application. The window class
            // and window name were obtained using the Spy++ tool.
            IntPtr calculatorHandle = FindWindow("CalcFrame","Calculator");
            // Verify that Calculator is a running process.
            if (calculatorHandle == IntPtr.Zero)
            {
                MessageBox.Show("Calculator is not running.");
                return;
            }
            // Make Calculator the foreground application and send it 
            // a set of calculations.
            SetForegroundWindow(calculatorHandle);
            SendKeys.SendWait("111");
            SendKeys.SendWait("*");
            SendKeys.SendWait("11");
            SendKeys.SendWait("=");
        }
        // Send a key to the button when the user double-clicks anywhere 
        // on the form.
        private void Form1_DoubleClick(object sender, EventArgs e)
        {
            // Send the enter key to the button, which raises the click 
            // event for the button. This works because the tab stop of 
            // the button is 0.
            SendKeys.Send("{ENTER}");
        }
    }
}