const uint WM_KEYDOWN = 0x100; const uint WM_KEYUP = 0x101; const int KeyDown = 0x0001; const int KeyUp = 0x0002; const byte VK_SPACE = 0x20; const byte SpaceSC = 39; private void button1_Click(object sender, EventArgs e) { Process p = (Process)comboBox1.SelectedItem; keybd_event(VK_SPACE, 0, Key_Down , 0); Thread.Sleep(50); PostMessage(p.MainWindowHandle, WM_KEYDOWN, (IntPtr)Keys.D1, IntPtr.Zero); PostMessage(p.MainWindowHandle, WM_KEYUP, (IntPtr)Keys.D1, IntPtr.Zero); keybd_event(VK_SPACE, 0, Key_Up, 0); }
using System; using System.Diagnostics; using System.Runtime.InteropServices; using System.Threading; using System.Windows.Forms; namespace KeysWithModefier { public class Class2 { private Process _p; [DllImport("user32.dll", SetLastError = true)] static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo); [DllImport("user32.dll")] public static extern IntPtr PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam); const uint WM_KEYDOWN = 0x100; const uint WM_KEYUP = 0x101; const uint Key_Down = 0x0001; const uint Key_Up = 0x0002; public Class2(Process p) { _p = p; } public void SendKeyWithModefir(Keys ModKey, Keys PressKey) { keybd_event((byte)ModKey, 0, 0, 0); Thread.Sleep(50); ; PostMessage(_p.MainWindowHandle, WM_KEYDOWN, (IntPtr)(PressKey), (IntPtr)(0)); PostMessage(_p.MainWindowHandle, WM_KEYUP, (IntPtr)(PressKey), (IntPtr)(0)); keybd_event((byte)ModKey, 0, Key_Up, 0); } ~Class2() { GC.Collect(); } } } ---------------------------------------------------------------------------------- using System; using System.Diagnostics; using System.Runtime.InteropServices; using System.Threading; using System.Windows.Forms; using KeysWithModefier; namespace WindowsFormsApp1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); LoadProcess(); } private void button1_Click(object sender, EventArgs e) { Class2 ccc = new Class2((Process)comboBox1.SelectedItem); ccc.SendKeyWithModefir(Keys.Space, Keys.D1); ccc = null; } private void LoadProcess() { comboBox1.Items.Clear(); comboBox1.DisplayMember = "MainWindowTitle"; foreach (Process item in Process.GetProcesses()) { try { if (!string.IsNullOrEmpty(item.MainWindowTitle)) comboBox1.Items.Add(item); } catch { } } } } }
InputSimulator sss = new InputSimulator(); Process p = (Process)comboBox1.SelectedItem; sss.Keyboard.KeyDown(VirtualKeyCode.SPACE); Thread.Sleep(50); PostMessage(p.MainWindowHandle, WM_KEYDOWN, (IntPtr)Keys.D1, IntPtr.Zero); PostMessage(p.MainWindowHandle, WM_KEYUP, (IntPtr)Keys.D1, IntPtr.Zero); sss.Keyboard.KeyUp(VirtualKeyCode.SPACE);