键盘钩子 用来截屏

。点击开始按钮 。a窗体隐藏,b窗体显示 scsysrq截图 。需要在桌面创建bc文件夹。

总的思路就是用钩子获取键盘信息 然后进行截图的线程控制

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;
        //全局的事件
        static int hKeyboardHook = 0; //键盘钩子句柄
        //鼠标常量
        public const int WH_KEYBOARD_LL = 13; //keyboard hook constant
        HookProc KeyboardHookProcedure; //声明键盘钩子回调事件.
        //声明键盘钩子的封送结构类型
        [StructLayout(LayoutKind.Sequential)]
        public class KeyboardHookStruct
        {
            public int vkCode; //表示一个在 1 到 254 间的虚似键盘码
            public int scanCode; //表示硬件扫描码
            public int fags;
            public int time;
            public int dwExtraInfo;
        }
        //装置钩子的函数
        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance,
        int threadId);
        //卸下钩子的函数
        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern bool UnhookWindowsHookEx(int idHook);
        //下一个钩挂的函数
        [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()
        {
            //安装键盘钩子
            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 (!(retKeyboard)) throw new Exception("UnhookWindowsHookEx failed.");
        }
        //键盘回调函数
        private int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam)
        {
            //lParam 参数只是数据的内存地址
            KeyboardHookStruct MyKeyboardHookStruct =
            (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct));
            Keys keyData = (Keys)MyKeyboardHookStruct.vkCode;
            if (wParam == WM_KEYDOWN || wParam == WM_KEYDOWN)
            {
                //PrintScreen 负责执行单次捕捉
                if (keyData == Keys.PrintScreen)
                {
                    capture_this_one_e.Set();
                }
                //End 负责捕捉的任务结束
                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);
            //图片路径用户须重新设置,才可正常运行
            String init_dir_fn = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            String dest_fn = null;
            //用事件的方法来抓获图片
            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();
            //初始捕获终止事件为未结束
            capture_terminate_e = new ManualResetEvent(false);
            //初始捕获终止状态为未结束
            capture_this_one_e = new ManualResetEvent(false);
            me_cap[0] = capture_terminate_e;
            me_cap[1] = capture_this_one_e;
            //启动捕捉线程
            ThreadStart workStart = new ThreadStart(Capture_screen);
            Thread workThread = new Thread(workStart);
            workThread.IsBackground = true;
            workThread.Start();

}

}
}

时间: 2024-10-10 06:48:19

键盘钩子 用来截屏的相关文章

小胖说事22-----iOS开发技巧之取消键盘响应和截屏功能

1.UILable内容模糊 在非Retina的iPad mini 的屏幕上,一个UILable的frame的origin值如果是有小数位(如0.5),就会造成显示模糊,所以最好还是用整数值的origin. 2.取消键盘响应 取消键盘响应事件,一般都会用到resignFirstResponder,但是还有三种方法可以取消键盘的响应. 第一种:重载UIViewcontroller中的touchesBegin方法,然后在里边执行 [self.view endEditing:YES]:这样单击UIVie

Snipaste强大离线/在线截屏软件的下载、安装和使用

步骤一: https://zh.snipaste.com/  ,去此官网下载. 步骤二:由于此是个绿色软件,直接解压即可. 步骤三:使用,见官网.ttps://zh.snipaste.com  按F1开始截屏 感谢下面哥的精彩微信文章 http://mp.weixin.qq.com/s?__biz=MzIwNzYwODYwMw==&mid=2247483903&idx=1&sn=02121fe920320bbe7b2fae012a18e70a&chksm=970e8f8ba

各手机截屏方法收集

很多时候需要把手机截屏保存下来,现收集各种方法以供使用. 一.把手机连接电脑,用豌豆荚.91助手.360手机助手等手机助手软件进行截图,这种方式的好处是,可以给手机的外型也“截个图”. 二.针对各手机自带的截屏功能如下: 1.三星手机截屏 1)同时按住手机中间的home键+电源键,等手机咔嚓一声,整个屏幕就会被截取下来.注意一定是同时按住,否则很有可能截取不下来. 2)在图册中有个screenshots相册用来存放截屏下来的图片 2.酷派手机截屏 1)安卓版本4.0以上 在手机上同时按住开机键和

Java开发的一个简单截屏工具

//源代码 import java.awt.*;import java.awt.datatransfer.DataFlavor;import java.awt.datatransfer.Transferable;import java.awt.datatransfer.UnsupportedFlavorException;import java.awt.event.*;import javax.swing.*;import java.io.*;import javax.imageio.*;imp

mac下截屏快捷键

在Mac OS中,有非常方便的截屏快捷键: Command-Shift-3: 对整个屏幕截屏,图片会保存的一个文件中(默认保存的桌面上) Command-Ctrl-Shift-3: 对整个屏幕截屏,图片被保存到剪贴板(夹纸板)中. Command-Shift-4: 对选定区域进行截屏,并将图片保存到文件中(默认保存的桌面上).在触发这个快捷键后,按空格(Space)键,可以对一整个窗口或菜单进行截屏. Command-Ctrl-Shift-4: 对选定区域进行截屏,图片被保存到剪贴板(夹纸板)中

Java代码截屏

使用java,借助Robot这个类可以实现软件截屏,代码如下: package test; import java.awt.AWTException; import java.awt.DisplayMode; import java.awt.GraphicsDevice; import java.awt.GraphicsEnvironment; import java.awt.Image; import java.awt.MenuItem; import java.awt.PopupMenu;

IMAC双系统WIN下截屏快捷键

一.Mac下有自带的截图功能 新Mac用户根本不必麻烦去寻找截屏软件,默认快捷键是 全屏幕截图:Command+Shift+3 区域截图:Command+Shift+4 随后按 Space(空格) 可以选定窗口截图 Mac下屏幕快照快捷键可以自定义设置: 系统偏好设置——键盘——键盘快捷键 Mac下截图默认保存在桌面上,格式为PNG: 二.苹果电脑在Win系统中,由于Macbook键盘上是没有Print Screen按键的 大家可以用如下方法截图: 全屏截图:fn+shift+F11 窗口截图:

Python写一个像QQ可快捷键唤起区域截屏的应用

1.截屏应用功能拆解 先把要实现哪些功能罗列一下,功能拆解好了,程序设计时就比较清晰. 1).首先要能响应快捷键调起截屏程序,像QQ使用Ctrl+shift+B可以截屏一样: 2).然后就是截图功能,可以用鼠标选取截取的区域: 3).最后是要把截屏保存为本地图片. 2.做一下技术调研 功能拆解出来了,就开始调查下实现难度,有没有现成的第三方库可用,Python是出了名的库多,多找一找总会找到的. 搜寻了一下Python有截图功能的库,基本都是全屏截图和程序输入坐标来区域截图,没办法用鼠标选择可视

C#使用phantomjs 进行网页整页截屏

hantomjs 是一个基于js的webkit内核无头浏览器 也就是没有显示界面的浏览器,这样访问网页就省去了浏览器的界面绘制所消耗的系统资源,比较适合用于网络测试等应用 .我只是调用了其中的一个截取网页的小功能,可以完美的解析网页的js和css 而且兼容html5,不过最新的1.5版本不支持flash,所以我采用了1.4的版本,能够得到完整的网页体验. 先看看执行的效率(4M电信,22:30点测试): phantomjs的目录结构 dll挺多的 都是必须的 codecs里面包含编码信息 qcn