简体   繁体   English

在 C# 中模拟按键?

[英]simulate a keypress in c#?

I searched the internet and in the fourum but cant find how to do it.我在互联网和 Fourum 中搜索过,但找不到如何操作。 what i want to do is to send a text in a discord chat every 5 minutes.我想要做的是每 5 分钟在不和谐聊天中发送一条文本。 and i want to send it from my account so i want to write a simple console app thet just output text and an enter to another programe.我想从我的帐户发送它,所以我想编写一个简单的控制台应用程序,它只输出文本并输入另一个程序。 I tried the sendInput class but dident get it to work.我尝试了 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);
        }
    }

How do i do that?我怎么做?

Did you try send message like this?您是否尝试过发送这样的消息?

List of WM messages WM 消息列表

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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM