C# 键盘监视+剪切板编程(每次复制都不一样哦!*v*)
由来:
前一段时间把收藏的google徽标都上传到博客上来,上传的时候发现不支持批量上传图片,而且上传的图片名称还不一样!!我靠!一百多张图片要弄到猴年马月才能上传完毕啊!!于是乎想办法偷懒:先下了一个批量更改文件名的工具,把图片通过改成了1~1百多.jpg,然后写了一个剪切板的程序来动态的更改每次复制的文件名,这样只需要每次CTRL+C然后点一下上传然后再CTRL+C点一下上传...
实现:
先一股脑的把代码全帖上来,然后稍微讲解下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Diagnostics;
namespace ClipboardEdit
{
public class InterceptKeys
{
private const int WH_KEYBOARD_LL = 13;
private const int WM_KEYDOWN = 0x0100;
private static LowLevelKeyboardProc _proc = HookCallback;
private static IntPtr _hookID = IntPtr.Zero;
/// <summary>
/// 启动监测程序
/// </summary>
public static void RunHook()
{
_hookID = SetHook(_proc);
}
/// <summary>
/// 关闭监测程序
/// </summary>
public static void UnHook()
{
UnhookWindowsHookEx(_hookID);
}
private static IntPtr SetHook(LowLevelKeyboardProc proc)
{
using (Process curProcess = Process.GetCurrentProcess())
using (ProcessModule curModule = curProcess.MainModule)
{
return SetWindowsHookEx(WH_KEYBOARD_LL, proc,GetModuleHandle(curModule.ModuleName), 0);
}
}
/// <summary>
/// 设置默认值与数据
/// </summary>
/// <param name="initInt"></param>
/// <param name="outString">"F:\图片\精品分类\品牌图片\google徽标\@_@.gif"</param>
public static void Init(int initInt, string outString)
{
i = initInt;
str = outString;
}
private static int i = 0;
private static string str = @"I:\over\Images\google\@_@.jpg";
private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
{
int vkCode = Marshal.ReadInt32(lParam);
Keys key=(Keys)vkCode;
//同时按下Ctrl+V键的时候
if (Control.ModifierKeys == Keys.Control && key.Equals(Keys.V))
{
//获得剪切板数据
string data = Clipboard.GetText(TextDataFormat.UnicodeText);
//重新设置剪切板数据
DataObject m_data = new DataObject();
m_data.SetData(DataFormats.Text, true, str.Replace("@_@",Convert.ToString(++i)));
Clipboard.SetDataObject(m_data, true);
}
}
return CallNextHookEx(_hookID, nCode, wParam, lParam);
}
#region 调用API
private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr SetWindowsHookEx(int idHook,
LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool UnhookWindowsHookEx(IntPtr hhk);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode,
IntPtr wParam, IntPtr lParam);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr GetModuleHandle(string lpModuleName);
#endregion
}
}
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Diagnostics;
namespace ClipboardEdit
{
public class InterceptKeys
{
private const int WH_KEYBOARD_LL = 13;
private const int WM_KEYDOWN = 0x0100;
private static LowLevelKeyboardProc _proc = HookCallback;
private static IntPtr _hookID = IntPtr.Zero;
/// <summary>
/// 启动监测程序
/// </summary>
public static void RunHook()
{
_hookID = SetHook(_proc);
}
/// <summary>
/// 关闭监测程序
/// </summary>
public static void UnHook()
{
UnhookWindowsHookEx(_hookID);
}
private static IntPtr SetHook(LowLevelKeyboardProc proc)
{
using (Process curProcess = Process.GetCurrentProcess())
using (ProcessModule curModule = curProcess.MainModule)
{
return SetWindowsHookEx(WH_KEYBOARD_LL, proc,GetModuleHandle(curModule.ModuleName), 0);
}
}
/// <summary>
/// 设置默认值与数据
/// </summary>
/// <param name="initInt"></param>
/// <param name="outString">"F:\图片\精品分类\品牌图片\google徽标\@_@.gif"</param>
public static void Init(int initInt, string outString)
{
i = initInt;
str = outString;
}
private static int i = 0;
private static string str = @"I:\over\Images\google\@_@.jpg";
private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
{
int vkCode = Marshal.ReadInt32(lParam);
Keys key=(Keys)vkCode;
//同时按下Ctrl+V键的时候
if (Control.ModifierKeys == Keys.Control && key.Equals(Keys.V))
{
//获得剪切板数据
string data = Clipboard.GetText(TextDataFormat.UnicodeText);
//重新设置剪切板数据
DataObject m_data = new DataObject();
m_data.SetData(DataFormats.Text, true, str.Replace("@_@",Convert.ToString(++i)));
Clipboard.SetDataObject(m_data, true);
}
}
return CallNextHookEx(_hookID, nCode, wParam, lParam);
}
#region 调用API
private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr SetWindowsHookEx(int idHook,
LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool UnhookWindowsHookEx(IntPtr hhk);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode,
IntPtr wParam, IntPtr lParam);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr GetModuleHandle(string lpModuleName);
#endregion
}
}
调用:
InterceptKeys.RunHook();
说明:
其实光显示或改变剪切板里面的数据没有这么多代码的,这里启动键盘监测程序,只有按下CTRL+C改会改变剪切板中的数据,像[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]这样的代码都是引入DLL,调用系统API用于监测键盘事件的。
结束语:
请把你所学的运用于生活,这样才会在快乐中学到东西,在学到东西的同时解决实际问题。按原来笨办法全部上传也用不了写这代码的时间的一半,但是你知道——我赚到了!!:)