当条件为true时激发keyevent的c#代码

本文关键字:keyevent 代码 条件 true | 更新日期: 2023-09-27 18:20:00

例如,我有一个项目(c#),它提供从1到5个数字的输出。如果是这样的话,我希望程序在记事本上写"A"。我没有使用FORM在c#中创建我的项目。它是用XML编写的。那么,我可以知道我该怎么做吗。?

比如,我试过-SendKeys.Send("{A}");当事件被激发时。但由于我的项目不是表格。我做不到。有人能帮我吗?:(

谢谢。

我的实际项目:我已经完成了情感识别。我已经完成了第二人生的情感。我想把(C#)中的情感检测输入到第二人生中。只剩下这个了。如果我已经澄清了我的疑虑,我可以这么做。谢谢。

当条件为true时激发keyevent的c#代码

为了将"A"放入记事本,您可以执行

  1. 查找记事本编辑窗口(假设NotePad.exe正在执行)
  2. 向找到的窗口发送WM_CHAR消息

代码可以是这样的要求的本机API声明:

  internal delegate Boolean EnumerationCallback(IntPtr handle, IntPtr parameter);
  [DllImport("User32.dll",
             CallingConvention = CallingConvention.Winapi,
             EntryPoint = "EnumWindows",
             CharSet = CharSet.Unicode,
             SetLastError = true)]
  [return: MarshalAs(UnmanagedType.Bool)]
  internal static extern Boolean EnumWindows(EnumerationCallback lpEnumFunc, 
    IntPtr lParam);
  [DllImport("User32.dll",
             CallingConvention = CallingConvention.Winapi,
             EntryPoint = "EnumChildWindows",
             CharSet = CharSet.Unicode,
             SetLastError = true)]
  [return: MarshalAs(UnmanagedType.Bool)]
  internal static extern Boolean EnumChildWindows(IntPtr hwndParent, 
    EnumerationCallback lpEnumFunc, 
    IntPtr lParam);
 [DllImport("user32.dll",
            CallingConvention = CallingConvention.Winapi,
            EntryPoint = "GetClassNameW",
            CharSet = CharSet.Unicode,
            SetLastError = true)]
  internal extern static int GetClassName(IntPtr hWnd,
   [MarshalAs(UnmanagedType.LPWStr)] 
    StringBuilder name,
    int size);
  [DllImport("User32.dll",
             CallingConvention = CallingConvention.Winapi,
             EntryPoint = "GetWindowThreadProcessId",
             SetLastError = true)]
  internal static extern uint GetWindowThreadProcessId(IntPtr hWnd, 
    out IntPtr ProcessId);
  [DllImport("User32.dll",
             EntryPoint = "SendMessageW",
             CallingConvention = CallingConvention.Winapi,
             SetLastError = true,
             CharSet = CharSet.Unicode)]
  internal extern static IntPtr SendMessage(IntPtr handle,
    int message,
    IntPtr wParam,
    IntPtr lParam);
  const int WM_CHAR = 0x102;

解决方案本身:

public void PrintA() {   
  IntPtr handleNotePad = IntPtr.Zero;   
  IntPtr handleNotePadEdit = IntPtr.Zero;
  // First, find NotePad Main window   
  EnumWindows(
   (IntPtr h, IntPtr p) => {
     int processId;
     GetWindowThreadProcessId(h, out processId);
     if (Process.GetProcessById(processId).ProcessName != "notepad")
       return true;
     handleNotePad = h;
     return false;
    }, 
    IntPtr.Zero);
  // Second find NotePad EDIT window as a child of NotePad Main window
  EnumChildWindows(handleNotePad,
    (IntPtr h, IntPtr p) => {
       StringBuilder s = new StringBuilder();
       s.Length = 500;
       GetClassName(h, s, s.Length);
       if (s.ToString() != "Edit")
         return true;
       handleNotePadEdit = h;
       return false;
    }, 
    IntPtr.Zero);
  // Finally, send the message
  SendMessage(handleNotePadEdit, WM_CHAR, (IntPtr) 'A', IntPtr.Zero);
}