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


// Check for Mouse Move Events                       
if (lastX!=Control.MousePosition.X ||
lastY!=Control.MousePosition.Y)
{
lastX = Control.MousePosition.X;
lastY = Control.MousePosition.Y;
      MouseMoveEventArgs mouseMoveInfo = new
      MouseMoveEventArgs(lastX,lastY);
      if (OnMouseMove!=null)
      {
            OnMouseMove(this,mouseMoveInfo);
      }

}

Once we’ve got Mouse movement out of the way, let’s move on to mouse buttons. This is done in much the same way, first we have our EventArgs object (With an enumerated type to help tell what the user is doing to the button)–


public enum MouseButtonState {Pressed,Released}
public class MouseButtonEventArgs : EventArgs
{
public MouseButtonEventArgs(MouseButtons 
 Button,MouseButtonState buttonState)
      {
            this.Button = Button;
            this.buttonState = buttonState;
      }
      public readonly MouseButtons Button;
      public readonly MouseButtonState buttonState;

}

Then we add our delegate, event, and any state variables required –

private MouseButtons lastButton = MouseButtons.None;
public delegate void MouseButtonHandler
(object inputListener,
      MouseButtonEventArgs mouseButtonInfo);

public event MouseButtonHandler OnMouseButton;

Then the implementation of the polling mechanism


if (lastButton!=Control.MouseButtons)
{                      
MouseButtonEventArgs mouseButtonInfo;
      if (Control.MouseButtons==MouseButtons.None)
      {
            mouseButtonInfo = new
MouseButtonEventArgs(
lastButton, MouseButtonState.Released);
            if (OnMouseButton!=null)
{
OnMouseButton( this,
 mouseButtonInfo);
            }
      }                                        
      else
      {
            mouseButtonInfo = new
MouseButtonEventArgs(              
Control.MouseButtons,
MouseButtonState.Pressed);
            if (OnMouseButton!=null)
      {
      OnMouseButton( this,
mouseButtonInfo);
            }                                  
      }
      lastButton=Control.MouseButtons;         
}

Finally, we move on to key press detection. Once again, we define our EventArgs variable


public class KeyPressEventArgs : EventArgs
{
public KeyPressEventArgs( Keys ModifierKeys,
 int KeyCode)
      {
            this.ModifierKeys= ModifierKeys;
            this.KeyCode = KeyCode;
      }
      public readonly Keys ModifierKeys;
      public readonly int KeyCode;

}

We must declare our Event and Delegate as in the other two cases, however, in this instance, instead of using state variables, we declare our Win32 API call.

Page 1  Page 3 




Google

Copyright 2017 Infinite Loop Ltd.