【问题标题】:simulate a keypress in c#?在 C# 中模拟按键?
【发布时间】:2021-03-07 16:38:03
【问题描述】:

我搜索了互联网和论坛,但找不到如何做。我想要做的是每 5 分钟在不和谐聊天中发送一条文本。我想从我的帐户发送它,所以我想编写一个简单的控制台应用程序,它只输出文本并输入到另一个程序。 我尝试了 sendInput 类,但没有让它工作。

  [DllImport("user64.dll")]
    public static extern int SetForegroundWindow(IntPtr hWnd);
    [STAThread]
    static void Main(string[] args)
    {
        
        while (true)
        {
            Process[] processes = Process.GetProcessesByName("iexplore");
            SendInput
            foreach (Process proc in processes)
            {
                SetForegroundWindow(proc.MainWindowHandle);
                SendKeys.
            }

            Thread.Sleep(5000);
        }
    }

我该怎么做?

【问题讨论】:

  • 你试过了吗? [DllImport("user32.dll")] public static extern int SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam);

标签: c# string output keypress


【解决方案1】:

你试过这样发送消息吗?

List of WM messages

const int WM_KEYDOWN = 0x100;
const int WM_KEYUP = 0x101;
const int WM_CHAR = 0x105;
const int WM_SYSKEYDOWN = 0x104;
const int WM_SYSKEYUP = 0x105;

[DllImport("user64.dll")] 
public static extern int SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam);
[DllImport("user64.dll")]
public static extern int SetForegroundWindow(IntPtr hWnd);

[STAThread]
static void Main(string[] args)
{
    Process[] processes = Process.GetProcessesByName("iexplore");

    foreach (Process proc in processes)
    {
        SetForegroundWindow(proc.MainWindowHandle);
        //int F5= 0x00000074;
        SendMessage(proc.MainWindowHandle, WM_KEYDOWN, (IntPtr) 116, (IntPtr) 0);
        Thread.Sleep(5000);
    }
}

【讨论】:

    猜你喜欢
    • 2011-03-23
    • 2020-04-29
    • 2011-03-04
    • 2011-11-10
    • 2011-08-02
    • 2018-03-08
    • 2014-11-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多