99 lines
3.0 KiB
C#
99 lines
3.0 KiB
C#
using System;
|
|
using System.Runtime.InteropServices;
|
|
using System.Text;
|
|
|
|
namespace HYDragCurveJig
|
|
{
|
|
public class KeyboardHook
|
|
{
|
|
[DllImport("user32.dll")]
|
|
private static extern int SetWindowsHookEx(int int_1, KeyboardHook.Delegate0 delegate0_1, IntPtr intptr_0, int int_2);
|
|
|
|
[DllImport("user32.dll")]
|
|
private static extern bool UnhookWindowsHookEx(int int_1);
|
|
|
|
[DllImport("user32.dll")]
|
|
private static extern int CallNextHookEx(int int_1, int int_2, int int_3, IntPtr intptr_0);
|
|
|
|
[DllImport("kernel32.dll")]
|
|
public static extern IntPtr GetModuleHandle(string lpModuleName);
|
|
|
|
[DllImport("kernel32.dll")]
|
|
public static extern int GetCurrentThreadId();
|
|
|
|
public void HookStart()
|
|
{
|
|
if (KeyboardHook.int_0 == 0)
|
|
{
|
|
this.delegate0_0 = new KeyboardHook.Delegate0(this.wTayyEwCc1);
|
|
IntPtr zero = IntPtr.Zero;
|
|
KeyboardHook.int_0 = KeyboardHook.SetWindowsHookEx(13, this.delegate0_0, zero, 0);
|
|
if (KeyboardHook.int_0 == 0)
|
|
{
|
|
this.HookStop();
|
|
throw new Exception("SetWindowsHookEx failed.");
|
|
}
|
|
}
|
|
}
|
|
|
|
[DllImport("user32.dll")]
|
|
private static extern IntPtr GetForegroundWindow();
|
|
|
|
[DllImport("user32.dll")]
|
|
private static extern int GetWindowText(IntPtr intptr_0, StringBuilder stringBuilder_0, int int_1);
|
|
|
|
private string method_0()
|
|
{
|
|
StringBuilder stringBuilder = new StringBuilder(256);
|
|
if (KeyboardHook.GetWindowText(KeyboardHook.GetForegroundWindow(), stringBuilder, 256) > 0)
|
|
{
|
|
return stringBuilder.ToString();
|
|
}
|
|
return null;
|
|
}
|
|
|
|
private int wTayyEwCc1(int int_1, int int_2, IntPtr intptr_0)
|
|
{
|
|
if (int_1 >= 0 && ((KeyboardHook.KeyBoardHookStruct)Marshal.PtrToStructure(intptr_0, typeof(KeyboardHook.KeyBoardHookStruct))).vkCode == 32 && this.method_0().IndexOf("Revit") != -1)
|
|
{
|
|
return 1;
|
|
}
|
|
return KeyboardHook.CallNextHookEx(KeyboardHook.int_0, int_1, int_2, intptr_0);
|
|
}
|
|
|
|
public void HookStop()
|
|
{
|
|
bool flag = true;
|
|
if (KeyboardHook.int_0 != 0)
|
|
{
|
|
flag = KeyboardHook.UnhookWindowsHookEx(KeyboardHook.int_0);
|
|
KeyboardHook.int_0 = 0;
|
|
}
|
|
if (!flag)
|
|
{
|
|
throw new Exception("UnhookWindowsHookEx failed.");
|
|
}
|
|
}
|
|
|
|
private static int int_0;
|
|
|
|
private KeyboardHook.Delegate0 delegate0_0;
|
|
|
|
[StructLayout(LayoutKind.Sequential)]
|
|
public class KeyBoardHookStruct
|
|
{
|
|
public int vkCode;
|
|
|
|
public int scanCode;
|
|
|
|
public int flags;
|
|
|
|
public int time;
|
|
|
|
public int dwExtraInfo;
|
|
}
|
|
|
|
private delegate int Delegate0(int nCode, int wParam, IntPtr lParam);
|
|
}
|
|
}
|