|
Please use FireFox to view this page
This website has been designed for use with the FireFox browser. Please use FireFox to view this page.
[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
Copyright 2012 Open Merchant Account Ltd.
|