Global mouse/keyboard hooks made by C #

Source: Internet
Author: User
Tags reserved

Today on the Internet to find a C # implementation of the interception of mouse, keyboard message sample code, learned a lot of things. Understand how to invoke Win32 APIs in C # .... Code as follows, more useful, do not dare to exclusive .... ^_^! Using System;
Using System.Runtime.InteropServices;
Using System.Reflection;
Using System.Threading;
Using System.Windows.Forms;
Using System.ComponentModel;

Namespace GMA. System.Windows
{
<summary>
This class allows your to tap keyboard and mouse and/or to detect their activity even
Application runes in background or does don't have any user interface at all. This class raises
Common. NET events with KeyEventArgs and MouseEventArgs, can easily retrieve any information.
</summary>
public class Useractivityhook
{
#region Windows Structure Definitions

<summary>
The point structure defines the X-and Y-coordinates's a point.
</summary>
<remarks>
Http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/rectangl_0tiq.asp
</remarks>
[StructLayout (LayoutKind.Sequential)]
Private class Point
{
<summary>
Specifies the x-coordinate of the point.
</summary>
public int x;
<summary>
Specifies the y-coordinate of the point.
</summary>
public int y;
}

<summary>
The MOUSEHOOKSTRUCT structure contains information about a mouse event passed to a wh_mouse hook procedure, Mouseproc .
</summary>
<remarks>
Http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing /hooks/hookreference/hookstructures/cwpstruct.asp
</remarks>
[StructLayout (LayoutKind.Sequential)]
Private Class MouseHookStruct
{
<summary>
Specifies a point structure which contains the X-and y-coordinates of the cursor, in screen coordinates.
</summary>
Public Point pt;
<summary>
Handle to the windows that would receive the mouse message corresponding to the mouse event.
</summary>
public int hwnd;
<summary>
Specifies the hit-test value. For a list of hit-test values, and the description of the WM_NCHITTEST message.
</summary>
public int whittestcode;
<summary>
Specifies extra information associated with the message.
</summary>
public int dwextrainfo;
}

<summary>
The MSLLHOOKSTRUCT structure contains information about a low-level keyboard input event.
</summary>
[StructLayout (LayoutKind.Sequential)]
Private Class Mousellhookstruct
{
<summary>
Specifies a point structure which contains the X-and y-coordinates of the cursor, in screen coordinates.
</summary>
Public Point pt;
<summary>
If the message is WM_MouseWheel, the High-order word of that is the wheel delta.
The Low-order word is reserved. A positive value indicates that of the wheel was rotated forward,
away from the user; A negative value indicates that the wheel is rotated backward, toward the user.
One wheel click is defined as Wheel_delta, which is 120.
If the message is Wm_xbuttondown, Wm_xbuttonup, WM_XBUTTONDBLCLK, Wm_ncxbuttondown, Wm_ncxbuttonup,
Or WM_NCXBUTTONDBLCLK, the High-order word specifies which X button was pressed or released,
And the Low-order word is reserved. This value can is one or more of the following values. Otherwise, Mousedata is not used.
XBUTTON1
The the "a" button was pressed or released.
XBUTTON2
The second X button was pressed or released.
</summary>
public int mousedata;
<summary>
Specifies the event-injected flag. An application can use the following value to test the mouse flags. Value Purpose
llmhf_injected Test the event-injected flag.
0
Specifies whether the event was injected. The value is 1 if the event was injected; Otherwise, it is 0.
1-15
Reserved.
</summary>
public int flags;
<summary>
Specifies the time stamp to this message.
</summary>
public int time;
<summary>
Specifies extra information associated with the message.
</summary>
public int dwextrainfo;
}


<summary>
The KBDLLHOOKSTRUCT structure contains information about a low-level keyboard input event.
</summary>
<remarks>
Http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing /hooks/hookreference/hookstructures/cwpstruct.asp
</remarks>
[StructLayout (LayoutKind.Sequential)]
Private Class Keyboardhookstruct
{
<summary>
Specifies a virtual-key code. The code must is a value in the range 1 to 254.
</summary>
public int vkcode;
<summary>
Specifies a hardware scan code for the key.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.