|
-
Jul 17th, 2010, 09:09 AM
#1
Thread Starter
New Member
Sendkey to webbrowser
Hi all,
I was wondering what is the right way to send keystroke to my webbrowser flash games? Basically I just need to compare the variable canale with nIstU.Value. If is larger, I will send "spacebar" to the flash game else I will send "right" to the flash game.
Below is my coding:
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace EEG_project_1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string[] g = File.ReadAllLines("port.txt"); //Reading the port number
serialPort1.PortName = g[0];
//Load Game
webBrowser1.Navigate(Application.StartupPath + "\\soopasprinta.swf");
}
byte[] Buff = new byte[15];
byte Bcont = 0;
byte Stato = 0;
int cont = 0;
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
int Canale;
if (serialPort1.IsOpen == false) return;
//The byte is managed in the buff array
while (serialPort1.BytesToRead > 0)
{
byte tmpB = (byte)serialPort1.ReadByte();
//Reading
if (Bcont == 0)
{//Synchronization
//Waiting for the synchronization is 0 or 1
if (tmpB >= 0xa5 || tmpB >= 0x5a)
Buff[Bcont++] = tmpB;
}
else
{
Buff[Bcont++] = tmpB;
if (tmpB >= 0xa5 || tmpB >= 0x5a)
{
Canale = (int)Buff[4] + (int)Buff[5];
// data management
if (Canale <= nIstD.Value)
{//Monitor if the value comes down under the threshold
cont = 0;
SendKeys.Send(" ");
}
else
{//Wait for the values to go back
cont++;
if (Canale >= nIstU.Value)
{//If the value returned above the threshold
//I send the key pressed
SendKeys.Send(" ");
}
}
}
Bcont = 3;
}
}
}
Any suggestion will be appreciated.
Thank you,
Louise
-
Jul 17th, 2010, 03:55 PM
#2
Frenzied Member
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}");
-
Jul 17th, 2010, 09:54 PM
#3
Thread Starter
New Member
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
-
Jul 19th, 2010, 03:27 PM
#4
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}.
-
Jul 19th, 2010, 03:33 PM
#5
Thread Starter
New Member
Re: Sendkey to webbrowser
I tried already, it give the same error message too.
-
Jul 19th, 2010, 10:18 PM
#6
Member
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.
-
Jul 20th, 2010, 03:45 PM
#7
Thread Starter
New Member
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
-
Jul 20th, 2010, 03:55 PM
#8
Member
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|