请看ProcessHotkey函数,本意是每次按下对应热键时模拟发送Ctrl+V,在当前光标处粘贴,可是发现只有第一次有效,跟踪发现每次按下热键都会到这个函数,可是后来的SendKeys.Send("^v");貌似就没用了,换成MessageBox.Show("Here am I");却每次都会成功;什么问题~~?
    public partial class Form1 : Form
    {
        [DllImport("user32.dll")]
        public static extern UInt32 RegisterHotKey(IntPtr hWnd, UInt32 id, UInt32 fsModifiers, Keys vk);        [DllImport("user32.dll", SetLastError = true)]
        public static extern bool UnregisterHotKey( IntPtr hWnd, int id );        public Form1()
        {
            InitializeComponent();
        }        private void Form1_Load(object sender, EventArgs e)
        {
            RegisterHotKey(Handle, 888, 3, Keys.D1);
        }        protected override void WndProc(ref Message m)
        {
            const int WM_HOTKEY = 0x0312;//按快捷键
            switch (m.Msg)
            {
                case WM_HOTKEY:
                    ProcessHotkey();//调用主处理程序
                    break;
            }
            base.WndProc(ref m);
        }        private void ProcessHotkey()
        {
            //Clipboard.SetText("Come on Man");
            //SendKeys.Send("^v");

            
            MessageBox.Show("Here am I");
        }        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            UnregisterHotKey(Handle, 888);
        }
    }

解决方案 »

  1.   

    添加一个应用程序配置文件,启用新SendKeys类实现。
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <appSettings>
      <add key="SendKeys" value="SendInput"/>
      </appSettings>
    </configuration>
      

  2.   

    The SendKeys class has been updated for the .NET Framework 3.0 to enable its use in applications that run on Windows Vista. The enhanced security of Windows Vista (known as User Account Control or UAC) prevents the previous implementation from working as expected.
    The SendKeys class is susceptible to timing issues, which some developers have had to work around. The updated implementation is still susceptible to timing issues, but is slightly faster and may require changes to the workarounds. The SendKeys class tries to use the previous implementation first, and if that fails, uses the new implementation. As a result, the SendKeys class may behave differently on different operating systems. Additionally, when the SendKeys class uses the new implementation, the SendWait method will not wait for messages to be processed when they are sent to another process.
    If your application relies on consistent behavior regardless of the operating system, you can force the SendKeys class to use the new implementation by adding the following application setting to your app.config file.
    <appSettings>
    <add key="SendKeys" value="SendInput"/>
    </appSettings>
    To force the SendKeys class to use the previous implementation, use the value "JournalHook" instead.