/// <summary>
/// 全局键盘事件
/// </summary>
public class KeyboardEvents
{
public static event Action<KeyEventArgs> OnKeyDown;
static KeyboardEvents()
{
EventManager.RegisterClassHandler(typeof(Window), UIElement.KeyDownEvent, (KeyEventHandler)onKeyDown);
}
private static void onKeyDown(object sender, KeyEventArgs e)
{
if (OnKeyDown != null)
OnKeyDown(e);
}
}
键盘钩子:
/// <summary>
/// 安装钩子函数
/// </summary>
/// <param name="idHook"></param>
/// <param name="lpfn"></param>
/// <param name="hInstance"></param>
/// <param name="threadId"></param>
/// <returns></returns>
[DllImport("user32.dll")]
public static extern int SetWindowsHookEx(
int idHook, //钩子的类型,即它处理的消息类型,可以用整型变量表示
HookProc lpfn, //钩子发挥作用时的委托类型的回调函数(当钩子得到系统消息时调用这个函数)
IntPtr hInstance, //应用程序实例的模块句柄 (要设置钩子的应用程序的句柄)
int threadId //与安装的钩子子程相关联的线程的标识符(如果为0即为全局钩子)
);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
private static extern IntPtr GetModuleHandle(string lpModuleName);
public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);//钩子委托
private int KeyboardHookProc(int nCode, IntPtr wParam, IntPtr lParam) //钩子子程
{
return 0;
//当钩子得到消息是执行这里的语句
//return 1 时代表将消息屏蔽拦截
//当return 0 或CallNextHookEx() 时代表不拦截次消息的执行
}
/// <summary>
/// 卸载钩子
/// </summary>
/// <param name="idHook"></param>
/// <returns></returns>
[DllImport("user32.dll")]
public static extern bool UnhookWindowsHookEx(
int idHook //钩子的类型
);
static int hHook = 0;
HookProc KeyboardHookProcedure;
public bool IfFocusPasswd;
public void Hook_Start() // 定义一个用来安装钩子的方法
{
if (hHook == 0)
{
KeyboardHookProcedure = new HookProc(KeyboardHookProc);//给委托变量赋初值
hHook = SetWindowsHookEx(
13, //此钩子的类型为全局键盘钩子 (14表示全局鼠标钩子)
KeyboardHookProcedure,//钩子子程(委托变量)
GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName),//表示全局钩子
0 //表示全局钩子
);
}
else //卸载键盘钩子
{
UnhookWindowsHookEx(hHook); // 卸载钩子
hHook = 0;
}
}
/// <summary>
/// 调用微软自带的屏幕键盘 (兼容win7和win10)
/// </summary>
public class TabTipHelper
{
private const int WM_SYSCOMMAND = 274;
private const uint SC_CLOSE = 61536;
private const uint SC_RESTORE = 0xF120;
private const uint SC_MAXIMIZE = 0xF030;
private const uint SC_MINIMIZE = 0xF020;
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern bool PostMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern bool PostMessage(IntPtr hWnd, int Msg, uint wParam, uint lParam);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern int RegisterWindowMessage(string lpString);
public static int ShowTaptip()
{
try
{
dynamic file = "C:\\Program Files\\Common Files\\microsoft shared\\ink\\TabTip.exe";
if (!System.IO.File.Exists(file))
return -1;
Process.Start(file);
return 1;
}
catch (Exception)
{
return 255;
}
}
public static async Task ShowTaptipAsync()
{
await Task.Run(() => ShowTaptip());
}
public static void CloseTaptip()
{
var touchhWnd = new IntPtr(0);
touchhWnd = FindWindow("IPTip_Main_Window", null);
if (touchhWnd == IntPtr.Zero)
return;
PostMessage(touchhWnd, WM_SYSCOMMAND, SC_CLOSE, 0);
}
}
C#键盘钩子拦截
于 2023-01-13 14:30:23 首次发布