我正在尝试将组合键发送到另一个程序,例如:
// keydown ctrl
SendMessage(windowBracketsKeyListener, 0x100, (IntPtr)VK_CONTROL, (IntPtr)0x001D0001);
// keydown S
SendMessage(windowBracketsKeyListener, 0x100, (IntPtr)VK_S, (IntPtr)0x001F0001);
SendMessage(windowBracketsKeyListener, 0x102, (IntPtr)115, (IntPtr)0);
// keyup ctrl
SendMessage(windowBracketsKeyListener, 0x101, (IntPtr)VK_CONTROL, (IntPtr)0xC01D0001);
到最后一行,我有一个错误(请看下面的图片)。
我发送的命令与Spy ++中的相同。因此,首先我自动尝试在窗口上单击CTRL + S,然后检查我在Spy ++中得到的内容并编写了相同的命令。
错误:
System.OverflowException: 'Arithmetic operation resulted in an overflow.'
伪造WM_KEYDOWN/WM_KEYUP
消息没有什么意义,仅发送它们生成的消息。仅两个WM_CHAR
消息。
请改为use SendKeys.Send(String) Method。
要指定应按住SHIFT,CTRL和ALT的任意组合, 按下其他几个键的同时,将其围起来 括号中的键。例如,要指定在按住SHIFT的同时 按下E和C,使用“ +(EC)”。指定在按住SHIFT的同时按住 按E,然后按C,不按SHIFT,使用“ + EC”。
以下示例对我有用:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace Test
{
static class Program
{
[DllImport("user32.dll")]
public static extern int SetForegroundWindow(IntPtr hWnd);
static void Main()
{
Process[] processes = Process.GetProcessesByName("notepad"); //notepad used be test
foreach (Process proc in processes)
{
SetForegroundWindow(proc.MainWindowHandle);
SendKeys.SendWait("^(s)");
}
}
}
}