C# Outlook 2013 Add-In Keyboard Events

最后都变了- 提交于 2019-12-08 11:27:51

问题


I'm working on a custom shortcut but I'm facing the problem, that the key events do not fire when outlook 2013 is focused (with 2007 & 2010 it's working fine). So far, I've tried different solutions. This one seems to be the best so far, the following code is a global hook.

[DllImport("user32.dll")]
static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc callback, IntPtr hInstance, uint threadId);

[DllImport("user32.dll")]
static extern bool UnhookWindowsHookEx(IntPtr hInstance);

[DllImport("user32.dll")]
static extern IntPtr CallNextHookEx(IntPtr idHook, int nCode, int wParam, IntPtr lParam);

[DllImport("kernel32.dll")]
static extern IntPtr LoadLibrary(string lpFileName);

private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);

const int WH_KEYBOARD_LL = 13;
const int WM_KEYDOWN = 0x100;

private LowLevelKeyboardProc _proc = hookProc;

private static IntPtr hhook = IntPtr.Zero;

public void SetHook()
{

  IntPtr hInstance = LoadLibrary("User32");
  hhook = SetWindowsHookEx(WH_KEYBOARD_LL, _proc, hInstance, 0);
}

public static IntPtr hookProc(int code, IntPtr wParam, IntPtr lParam)
{
  Debug.WriteLine("key event triggered");
  //...
  return CallNextHookEx(hhook, code, (int)wParam, lParam);
}

来源:https://stackoverflow.com/questions/31647898/c-sharp-outlook-2013-add-in-keyboard-events

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!