我试图在用户在我的加载项中键入Word时捕获事件,并且我有以下代码
delegate IntPtr HookProc(int nCode, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", CharSet=CharSet.Auto)]
static extern IntPtr SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hMod, int dwThreadId);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr GetModuleHandle(string lpModuleName);
[DllImport("user32.dll")]
static extern bool UnhookWindowsHookEx(IntPtr hhk);
[DllImport("user32.dll")]
static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
const int WH_KEYBOARD_LL = 13;
const int WM_KEYDOWN = 256;
static IntPtr hook;
static void Main()
{
HookProc hp = new HookProc(HookCallback);
hook = SetWindowsHookEx( WH_KEYBOARD_LL, hp, GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName), 0 );
//UnhookWindowsHookEx( hook );
//GC.KeepAlive( hp );
}
private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
{
int pointerCode = Marshal.ReadInt32(lParam);
string pressedKey = ((Keys)pointerCode).ToString();
var thread = new Thread(() => { MessageBox.Show(pressedKey); });
thread.Start();
}
return CallNextHookEx(hook, nCode, wParam, lParam);
}
通常它在我键入任何应用程序时都有效,除了在运行加载项的MS Word实例中。任何想法为什么被忽略了?
由于
答案 0 :(得分:1)
我有一个类似的问题,但唯一的解决方案(和一个坏的解决方案)似乎是:
(其他大卫) - 如果你有一个更好的解决方案,你能否回应这个问题,正如我上面概述的那样,虽然它有效,但却非常糟糕。我希望有一个更好的解释,为什么Word阻止在应用程序中发出的按键。