Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
Available now!
Buy at Amazon US or
Buy at Amazon UK


» Windows API reference
» Webcam streaming in VB.NET
» Remoting with firewalls
» RSA from first principles
» Key & MouseLogger in .NET
» Networking Resource Kit for .NET
» Migrating VB6 Winsock to VB.NET
» Migrating C++ sockets to C#
» RFC Reference guide
» COM Reference guide
» WMI Reference guide
» SQL stored procedures
» TCP & UDP port reference
» NET Framework reference
» Ethernet Type codes
» MAC address assignments
» DLL entry point reference
» Boost SQL performance
» Free SMS UK
» Free SMS Ireland
» Free SMS South Africa
» Internet Explorer

Contact us


public static extern int GetAsyncKeyState (long vKey);

public delegate void KeyPressHandler
      (object inputListener,
      KeyPressEventArgs KeyPressInfo);

public event KeyPressHandler OnKeyPress;

The polling implementation can now be added

// check for key presses
int i=0;
if (GetAsyncKeyState(i) ==  Int16.MinValue+1 )
            KeyPressEventArgs KeyPressInfo =
new KeyPressEventArgs(
            if (OnKeyPress!=null)
{                 OnKeyPress(this,KeyPressInfo);


To finish off the class, you need to include some standard assemblies at the head of the code.

using System;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Threading;

using System.Windows.Forms;

With the InputListener class out of the way, we can now look at the client application. The user interface for this application consists solely of a textbox named tbStatus, of which its multiline property is set to true. Double click on the form, and add the following code to the Form_Load event.

private void Form1_Load(object sender, System.EventArgs e)
      InputListener inputListener = new
      inputListener.OnMouseMove += new
InputListener.MouseMoveHandler( InputListener_MouseMove);
      inputListener.OnMouseButton += new
      inputListener.OnKeyPress += new

The Form_Load event creates a new InputListener Class, assigns functions to each one of its events, and then calls the Run method to start the loop. You must now implement each one of the functions, and have them output to the screen. The first one we shall code is the KeyPress event handler. This displays the pressed key on-screen, and scrolls the text window down to the bottom for readability.

private void InputListener_KeyPress(
object sender, KeyPressEventArgs e)
if (e.ModifierKeys == Keys.Shift)
            tbStatus.Text += "Pressed Shift & " +
 Convert.ToChar(e.KeyCode) +
            tbStatus.Text += "Pressed " +
+ "\r\n";              
      tbStatus.SelectionStart = tbStatus.Text.Length;

The same is done for the mouse button event handler.

Page 2  Page 4 


Copyright 2019 Infinite Loop Ltd.