I try to use PostMasseg to send VK to inactive application i want to sent (Space+1)
but it always send only (1)
here what spy++ read whine i press (space+1)
<000001> 000702FC P WM_KEYDOWN nVirtKey:VK_SPACE cRepeat:1 ScanCode:39 fExtended:0 fAltDown:0 fRepeat:0 fUp:0<br />
<br />
<000002> 000702FC P WM_CHAR chCharCode:'32' (32) cRepeat:1 ScanCode:39 fExtended:0 fAltDown:0 fRepeat:0 fUp:0<br />
<br />
<000003> 000702FC P WM_KEYDOWN nVirtKey:'1' cRepeat:1 ScanCode:02 fExtended:0 fAltDown:0 fRepeat:0 fUp:0<br />
<br />
<000004> 000702FC P WM_CHAR chCharCode:'49' (49) cRepeat:1 ScanCode:02 fExtended:0 fAltDown:0 fRepeat:0 fUp:0<br />
<br />
<000005> 000702FC P WM_KEYUP nVirtKey:VK_SPACE cRepeat:1 ScanCode:39 fExtended:0 fAltDown:0 fRepeat:1 fUp:1<br />
<br />
<000006> 000702FC P WM_KEYUP nVirtKey:'1' cRepeat:1 ScanCode:02 fExtended:0 fAltDown:0 fRepeat:1 fUp:1<br />
<br />
<000007> 000702FC P WM_SYSKEYDOWN nVirtKey:VK_MENU cRepeat:1 ScanCode:38 fExtended:0 fAltDown:1 fRepeat:0 fUp:0
how to simulate this?
now i have something else i didn't under stand i didn't get what i need with this code
public static void sendKey(IntPtr hwnd, Keys keyCode)
{
uint scanCode = MapVirtualKey((uint)keyCode, 0);
uint lParam;
lParam = (0x00000001 | (scanCode << 16));
PostMessage(hwnd, (UInt32)WM_KEYDOWN, (IntPtr)keyCode, (IntPtr)lParam);
Thread.Sleep(100);
uint scanCode1 = MapVirtualKey((uint)Keys.D1, 0);
uint lParam1;
lParam1 = (0x00000001 | (scanCode1 << 16));
PostMessage(hwnd, (UInt32)WM_KEYDOWN, (IntPtr)Keys.D1, (IntPtr)lParam1);
Thread.Sleep(50);
PostMessage(hwnd, WM_KEYUP, (IntPtr)Keys.D1, (IntPtr)(0xC0020001));
PostMessage(hwnd, WM_KEYUP, (IntPtr)keyCode, (IntPtr)(0xC0390001));
}
and spy++ read this from my app when i press the command button
<000001> 000401BA P WM_KEYDOWN nVirtKey:VK_SPACE cRepeat:1 ScanCode:39 fExtended:0 fAltDown:0 fRepeat:0 fUp:0
<000002> 000401BA P WM_CHAR chCharCode:'32' (32) cRepeat:1 ScanCode:39 fExtended:0 fAltDown:0 fRepeat:0 fUp:0
<000003> 000401BA P WM_KEYDOWN nVirtKey:'1' cRepeat:1 ScanCode:02 fExtended:0 fAltDown:0 fRepeat:0 fUp:0
<000004> 000401BA P WM_CHAR chCharCode:'49' (49) cRepeat:1 ScanCode:02 fExtended:0 fAltDown:0 fRepeat:0 fUp:0
<000005> 000401BA P WM_KEYUP nVirtKey:'1' cRepeat:1 ScanCode:02 fExtended:0 fAltDown:0 fRepeat:1 fUp:1
<000006> 000401BA P WM_KEYUP nVirtKey:VK_SPACE cRepeat:1 ScanCode:39 fExtended:0 fAltDown:0 fRepeat:1 fUp:1
but it didn't do what i need ?
can any one tell me where's the error
i don it with this code
<pre>using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
[DllImport("user32.dll")]
public static extern IntPtr PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern uint MapVirtualKey(uint uCode, uint uMapType);
[DllImport("user32.dll", SetLastError = true)]
static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
const uint WM_KEYDOWN = 0x0100;
const uint WM_KEYUP = 0x0101;
const int WM_SYSKEYDOWN = 0x0104;
const int KEYEVENTF_EXTENDEDKEY = 0x0001;
const int KEYEVENTF_KEYUP = 0x0002;
public Form1()
{
InitializeComponent();
LoadProcess();
}
private void button1_Click(object sender, EventArgs e)
{
Process p = (Process)comboBox1.SelectedItem;
keybd_event((byte)Keys.Space, (byte)0x02, (int)WM_KEYDOWN, 0);
sendKeyUp(p.MainWindowHandle, Keys.D1);
sendKeyDown(p.MainWindowHandle, Keys.D1);
keybd_event((byte)Keys.Space, (byte)0x82, KEYEVENTF_KEYUP, 0);
}
private void LoadProcess()
{
comboBox1.Items.Clear();
comboBox1.DisplayMember = "MainWindowTitle";
foreach (Process item in Process.GetProcesses())
{
try
{
comboBox1.Items.Add(item);
}
catch { }
}
}
public static void sendKeyDown(IntPtr hwnd, Keys keyCode)
{
uint scanCode = MapVirtualKey((uint)keyCode, 0);
uint lParam;
lParam = (0x00000001 | (scanCode << 16));
PostMessage(hwnd, (UInt32)WM_SYSKEYDOWN, (IntPtr)keyCode, (IntPtr)lParam);
}
public static void sendKeyUp(IntPtr hwnd, Keys keyCode)
{
uint scanCode = MapVirtualKey((uint)keyCode, 0);
uint lParam;
lParam = (0xC0000001 | (scanCode << 16));
PostMessage(hwnd, WM_KEYUP, (IntPtr)keyCode, (IntPtr)lParam);
}
}
}
but still there is error
the app didn't stop it keep repeat the key pressing
What I have tried:
const uint WM_KEYDOWN = 0x100;
const uint WM_KEYUP = 0x0101;
PostMessage(p.MainWindowHandle, WM_KEYDOWN, (IntPtr)Keys.Space, (IntPtr)0x0);
PostMessage(p.MainWindowHandle, WM_KEYDOWN, (IntPtr)Keys.D1, (IntPtr)0x0);
PostMessage(p.MainWindowHandle, WM_KEYUP, (IntPtr)Keys.Space, (IntPtr)0x0);
PostMessage(p.MainWindowHandle, WM_KEYUP, (IntPtr)Keys.D1, (IntPtr)0x0);