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



Articles

» 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


[DllImport("user32.dll")]

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;
for(i=1;i<Byte.MaxValue;i++)
{
if (GetAsyncKeyState(i) ==  Int16.MinValue+1 )
      {
            KeyPressEventArgs KeyPressInfo =
new KeyPressEventArgs(
            Control.ModifierKeys,i);
            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();
      inputListener.OnMouseMove += new
InputListener.MouseMoveHandler( InputListener_MouseMove);
      inputListener.OnMouseButton += new
InputListener.MouseButtonHandler(
InputListener_MouseButton);              
      inputListener.OnKeyPress += new
InputListener.KeyPressHandler(
InputListener_KeyPress);                 
      inputListener.Run();
}

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) +
 "\r\n";               
      }
      else
      {
            tbStatus.Text += "Pressed " +
Convert.ToChar(
e.KeyCode).ToString().ToLower()
+ "\r\n";              
      }
      tbStatus.SelectionStart = tbStatus.Text.Length;
      tbStatus.ScrollToCaret();
}

The same is done for the mouse button event handler.

Page 2  Page 4 




Google

Copyright 2015 Open Merchant Account Ltd.