Keyboard hooks are used to intercept the screen

Source: Internet
Author: User

。 Click the Start button. A form is hidden, and the B form displays SCSYSRQ. You need to create a BC folder on your desktop.

The general idea is to use the hook to obtain the keyboard information and then the line program control system

Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Diagnostics;
Using System.Drawing;
Using System.Linq;
Using System.Runtime.InteropServices;
Using System.Text;
Using System.Threading;
Using System.Windows.Forms;

Namespace ex12
{
public partial class Form1:form
{
Public Form1 ()
{
InitializeComponent ();
}

[DllImport ("User32.dll", EntryPoint = "SendMessage")]
private static extern int SendMessage (
IntPtr hWnd,//Handle to Destination window
int MSG,//message
int WParam,//? RST message parameter
int LParam//second message parameter
);
protected override void Defwndproc (ref Message m)
{
Switch (m.msg)
{
Case HIDDEN_MM:
This. Hide ();
Break
Case SHOW:
This. Visible = true;

Break
Default
Base. Defwndproc (ref m);
Break
}
}


public static INTPTR HWND;
public const int HIDDEN_MM = 0x500;
public const int SHOW = 0x521;
Private Const int WM_KEYDOWN = 0x100;
Private Const int WM_KEYUP = 0X101;
Private Const int wm_syskeydown = 0x104;
Private Const int wm_syskeyup = 0x105;
Private Const int vk_snapshot = 0x2C;
Event of the Global
static int hkeyboardhook = 0; Keyboard hook handle
Mouse Constants
public const int WH_KEYBOARD_LL = 13; Keyboard Hook constant
HookProc keyboardhookprocedure; Declares a keyboard hook callback event.
Declaring the type of marshaling structure for a keyboard hook
[StructLayout (LayoutKind.Sequential)]
public class Keyboardhookstruct
{
public int vkcode; Represents a virtual-like keyboard code between 1 and 254
public int scancode; Indicates a hardware scan code
public int fags;
public int time;
public int dwextrainfo;
}
function of the device hooks
[DllImport ("user32.dll", CharSet = CharSet.Auto, callingconvention = Callingconvention.stdcall)]
public static extern int SetWindowsHookEx (int idhook, HookProc lpfn, IntPtr hinstance,
int threadId);
function to remove hooks
[DllImport ("user32.dll", CharSet = CharSet.Auto, callingconvention = Callingconvention.stdcall)]
public static extern bool UnhookWindowsHookEx (int idhook);
The next hook-and-peg function
[DllImport ("user32.dll", CharSet = CharSet.Auto, callingconvention = Callingconvention.stdcall)]
public static extern int CallNextHookEx (int idhook, int nCode, Int32 wParam, IntPtr lParam);
[DllImport ("user32")]
public static extern int toascii (int uvirtkey, int uscancode, byte[] lpbkeystate, byte[]
Lpwtranskey, int fustate);
[DllImport ("user32")]
public static extern int getkeyboardstate (byte[] pbkeystate);
[DllImport ("kernel32.dll", CharSet = CharSet.Auto, callingconvention = Callingconvention.stdcall)]
private static extern IntPtr GetModuleHandle (string lpmodulename);
[DllImport ("kernel32.dll")]
static extern int GetTickCount ();
public delegate int HookProc (int nCode, Int32 wParam, IntPtr lParam);
public static ManualResetEvent Capture_terminate_e;
public static ManualResetEvent Capture_this_one_e;

public static manualresetevent[] Me_cap = new manualresetevent[2];
private void Start_h ()
{
Installing the keyboard hooks
if (Hkeyboardhook = = 0)
{
Keyboardhookprocedure = new HookProc (KEYBOARDHOOKPROC);
Process curprocess = process.getcurrentprocess ();
Processmodule curmodule = Curprocess.mainmodule;
Hkeyboardhook = SetWindowsHookEx (Wh_keyboard_ll, Keyboardhookprocedure, GetModuleHandle (CurModule.ModuleName), 0);
if (Hkeyboardhook = = 0)
{
Stop_h ();
throw new Exception ("SetWindowsHookEx is failed.");
}
}
}
private void Stop_h ()
{
bool Retkeyboard = true;
if (Hkeyboardhook! = 0)
{
Retkeyboard = UnhookWindowsHookEx (Hkeyboardhook);
Hkeyboardhook = 0;
}
If removing the hook fails
if (! ( Retkeyboard)) throw new Exception ("UnhookWindowsHookEx failed.");
}
Keyboard callback function
private int Keyboardhookproc (int nCode, Int32 wParam, IntPtr LParam)
{
The LParam parameter is just the memory address of the data
Keyboardhookstruct mykeyboardhookstruct =
(keyboardhookstruct) Marshal.PtrToStructure (LParam, typeof (Keyboardhookstruct));
Keys keyData = (keys) Mykeyboardhookstruct.vkcode;
if (WParam = = Wm_keydown | | wParam = = WM_KEYDOWN)
{
Printscreen is responsible for performing a single capture
if (KeyData = = Keys.printscreen)
{
Capture_this_one_e.set ();
}
End the task that is responsible for capturing
if (KeyData = = keys.end)
{
Capture_terminate_e.set ();
}
if (KeyData = = KEYS.A)
{
SendMessage (HWND, hidden_mm, 0, 0);
}
if (KeyData = = keys.b)
{
SendMessage (HWND, SHOW, 0, 0);
}
}
Return CallNextHookEx (Hkeyboardhook, NCode, WParam, LParam);
}
static void Capture_screen ()
{
int s_wid = Screen.PrimaryScreen.Bounds.Width;
int s_height = Screen.PrimaryScreen.Bounds.Height;
Bitmap b_1 = new Bitmap (S_wid, s_height);
Graphics g_ = Graphics.fromimage (b_1);
Picture path user must be reset to run properly
String INIT_DIR_FN = Environment.getfolderpath (Environment.SpecialFolder.Desktop);
String DEST_FN = null;
Use the event method to capture the picture
int index = WaitHandle.WaitAny (Me_cap, 500);
while (Index! = 0)
{
if (index = = 1)
{
DEST_FN = INIT_DIR_FN;
DEST_FN + = "\\bc\\";
DEST_FN + = GetTickCount (). ToString ();
DEST_FN + = "ab.bmp";
G_. CopyFromScreen (0, 0, 0, 0, New Size (S_wid, s_height));
B_1.save (DEST_FN, SYSTEM.DRAWING.IMAGING.IMAGEFORMAT.BMP);
Capture_this_one_e.reset ();
}
index = WaitHandle.WaitAny (Me_cap, 500);
}
G_. Dispose ();
B_1.dispose ();
}

private void Form1_Load (object sender, EventArgs e)
{

HWND = this. Handle;
ll = new ManualResetEvent (false);


}


private void Button1_Click (object sender, EventArgs e)
{

Start_h ();
Initial capture termination event is not closed
Capture_terminate_e = new ManualResetEvent (false);
Initial capture termination status is not closed
Capture_this_one_e = new ManualResetEvent (false);
Me_cap[0] = capture_terminate_e;
ME_CAP[1] = capture_this_one_e;
Start the snapping thread
ThreadStart Workstart = new ThreadStart (capture_screen);
Thread workthread = new Thread (Workstart);
Workthread.isbackground = true;
Workthread.start ();



}


}
}

Keyboard hooks are used to intercept the screen

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.