using System;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using System.Runtime.InteropServices;
using System.Windows;
// This line is not mandatory, but improves loading performances
[assembly: CommandClass(typeof(WinHook.MyCommands))]
namespace WinHook
{
public class MyCommands
{
[DllImport("accore.dll",
CharSet = CharSet.Unicode,
CallingConvention = CallingConvention.Cdecl,
EntryPoint = "?acedRegisterFilterWinMsg@@YA_NQ6A_NPEAUtagMSG@@@Z@Z")] //с версии AutoCAD2019
private static extern bool acedRegisterFilterWinMsg(WindowHookProc callBackFunc);
[DllImport("accore.dll", CharSet = CharSet.Unicode,
CallingConvention = CallingConvention.Cdecl,
EntryPoint = "?acedRemoveFilterWinMsg@@YA_NQ6A_NPEAUtagMSG@@@Z@Z")]
private static extern bool acedRemoveFilterWinMsg(WindowHookProc callBackFunc);
const int WM_KEYDOWN = 0x100; // Нажатие клавиши
const int WM_KEYUP = 0x101; // Отжатие клавиши
const int VK_ADD = 0x6B; // Клавиша +
const int VK_OEM_PLUS = 0xBB; // Клавиша + на цифровой клавиатуре
// Функция обратного вызова для хука
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate bool WindowHookProc(ref System.Windows.Forms.Message msg);
private static bool WindowsHook(
ref System.Windows.Forms.Message msg)
{
// проверяем структуру msg на то, что нас интересует,
// например, клавиши, движения мыши, и т.д.
if (msg.Msg == WM_KEYDOWN)
{
if (msg.WParam == (IntPtr)VK_ADD || msg.WParam == (IntPtr)VK_OEM_PLUS)
// делаем что нужно
MessageBox.Show("Нажата клавиша +");
}
return false;
}
private static WindowHookProc callBackFunc = null;
[CommandMethod("registerHook")]
public static void CmdRegisterHook()
{
if (callBackFunc != null)
{
acedRemoveFilterWinMsg(callBackFunc);
}
callBackFunc = new WindowHookProc(WindowsHook);
acedRegisterFilterWinMsg(callBackFunc);
}
[CommandMethod("UnregisterHook")]
public static void CmdUnRegisterHook()
{
if (callBackFunc != null)
{
acedRemoveFilterWinMsg(callBackFunc);
callBackFunc = null;
}
}
}
}