Re: Sendkey to webbrowser
If I am understanding you correctly you don't want to send the text " ", you want to send the space bar keystroke.
Code:
SendKeys.Send("{SPACE BAR}");
Re: Sendkey to webbrowser
Ya, I am trying to send the spacebar, but the error says that:
Code:
SendKeys cannot run inside this application because the application is not handling Windows messages. Either change the application to handle messages, or use the SendKeys.SendWait method.
Is there any other way else I could do this?
Regards,
Louise:confused:
Re: Sendkey to webbrowser
Well, since it tells you to, why not try:
C# Code:
SendKeys.SendWait("{SPACE}"); // and I'm pretty sure it's {SPACE} and not {SPACE BAR}.
Re: Sendkey to webbrowser
I tried already, it give the same error message too.
Re: Sendkey to webbrowser
I wouldn't use sendkeys. It requires that the window have focus. Alternatively use some api.
Declarations like in vb.
Find the parent window and child window then use postmessage to send the key.
Code:
using System.Runtime.InteropServices;
[DllImport("user32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Ansi)]
public static extern IntPtr FindWindow(string className, string windowName);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, IntPtr windowTitle);
[DllImport("User32.Dll", EntryPoint = "PostMessageA")]
static extern bool PostMessage(IntPtr hWnd, uint msg, int wParam, IntPtr lParam);
private const int VK_SPACE = 32;
private const int WM_KEYDOWN = 0x100;
private const int WM_KEYUP = 0x101;
Try to figure a the rest out. That should point you in the right direction.
Re: Sendkey to webbrowser
Hi SC,
Thanks for your reply, I tried the code already, but I am not sure whether I did it the right way.
Code:
while (serialPort1.BytesToRead > 0)
{
IntPtr sprint = FindWindow("\\soopasprinta.swf",null);
byte tmpB = (byte)serialPort1.ReadByte();
if (Bcont == 0)
{
if (tmpB >= 0x5A)
Buff[Bcont++] = tmpB;
}
else
{
Buff[Bcont++] = tmpB;
if (tmpB >= 0x5A)
{
Canale = (int)Buff[4] + (int)Buff[5];
Bcont= Bcont--;
if (Stato == 0)
{
if (Canale <= nIstD.Value)
{
Stato = 1;
cont = 0;
PostMessage(sprint, WM_KEYDOWN, VK_SPACE, IntPtr.Zero);
PostMessage(sprint, WM_KEYUP, VK_SPACE, IntPtr.Zero);
}
}
else
{
cont++;
if (Canale >= nIstU.Value)
{
Stato = 0;
PostMessage(sprint, WM_KEYDOWN, VK_RIGHT, IntPtr.Zero);
PostMessage(sprint, WM_KEYUP, VK_RIGHT, IntPtr.Zero);
}
}
} Bcont = 3;
}
}
}
I am not really an expert in C#, may I know how do we get the parent window and child window name? I am not so sure if I got it right here, as I assume the parent window is the form1 title and my child window is the flash game name soopasprinta?
Regards,
Louise:afrog:
Re: Sendkey to webbrowser
Assuming form1 is the main window that contains the game in question. Then yes the parent window is form1.
IntPtr sprint = FindWindow("the main window class name",null); find out the main window class name, I use a api spy to find this out.
IntPtr sprintA = FindWindowEx(sprint , IntPtr.Zero, "child window class name as string", IntPtr.Zero);
Find out the child window class name and input it as a string as shown above. I think that should work. I'm no pro. But, maybe it will help you out.
If you look at my thread unable to get window text. You should get a pretty good idea on what I'm talking about. Although the api spy I use is for vb. I use it to spy on windows.