C#2013年Office中的全球键盘挂钩插件
我遇到了一个问题,无法使我的Office Addin在Powerpoint 2013上使用我的全局键盘,但在以前的版本(2007和2010)上无法使用。C#2013年Office中的全球键盘挂钩插件,c#,windows,office-addins,keyboard-hook,powerpoint-2013,C#,Windows,Office Addins,Keyboard Hook,Powerpoint 2013,我遇到了一个问题,无法使我的Office Addin在Powerpoint 2013上使用我的全局键盘,但在以前的版本(2007和2010)上无法使用。 我没有得到任何例外,但Powerpoint 2013上似乎从未触发过OnKeyDown事件,我不知道为什么 我在所有版本的Windows(XP、7、8和8.1)、32位和64位环境中都遇到同样的问题。Microsoft Office的版本为32位 下面是一个代码示例: using System; using System.Collections
我没有得到任何例外,但Powerpoint 2013上似乎从未触发过
OnKeyDown
事件,我不知道为什么
我在所有版本的Windows(XP、7、8和8.1)、32位和64位环境中都遇到同样的问题。Microsoft Office的版本为32位
下面是一个代码示例:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Office = Microsoft.Office.Core;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace testHook
{
public partial class ThisAddIn
{
Hook hook;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
hook = new Hook(Hook.HookType.KeyBoard, Hook.HookVisibility.Global);
hook.OnKeyDown += new KeyEventHandler(hook_OnKeyDown);
hook.Start();
}
void hook_OnKeyDown(object sender, KeyEventArgs e)
{
MessageBox.Show(e.KeyCode.ToString());
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
hook.Stop();
hook = null;
}
#region VSTO generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
}
#endregion
class Hook
{
private IntPtr m_Hook = (IntPtr)0;
private HookVisibility m_Visibility;
private HookType m_HookType;
private HookProc m_Proc;
public enum HookType { KeyBoard };
public enum KeyBoardEventType { KeyDown, KeyUp, SysKeyDown, SysKeyUp, KeyShift, KeyCapital, NumLock };
public enum HookVisibility { Global, Local };
private delegate IntPtr HookProc(int nCode, int wParam, IntPtr lParam);
private KeyPressEventHandler m_onKeyPress;
private KeyEventHandler m_onKeyUp;
private KeyEventHandler m_onKeyDown;
public event KeyPressEventHandler OnKeyPress
{
add
{
m_onKeyPress += value;
}
remove
{
m_onKeyPress -= value;
}
}
public event KeyEventHandler OnKeyUp
{
add
{
m_onKeyUp += value;
}
remove
{
m_onKeyUp -= value;
}
}
public event KeyEventHandler OnKeyDown
{
add
{
m_onKeyDown += value;
}
remove
{
m_onKeyDown -= value;
}
}
#region DLLImport
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, SetLastError = true)]
private static extern IntPtr SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hmod, int dwThreadId);
[DllImport("user32.dll")]
private static extern IntPtr CallNextHookEx(IntPtr hHook, int nCode, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, SetLastError = true)]
private static extern bool UnhookWindowsHookEx(IntPtr hHook);
[DllImport("Kernel32.dll", SetLastError = true)]
private static extern IntPtr GetModuleHandle(IntPtr lpModuleName);
[DllImport("Kernel32.dll", SetLastError = true)]
private static extern IntPtr GetModuleHandle(String lpModuleName);
[DllImport("Kernel32.dll")]
private static extern IntPtr GetCurrentThreadId();
[DllImport("user32")]
private static extern int ToAscii(int uVirtKey, int uScanCode, byte[] lpbKeyState, byte[] lpwTransKey, int fuState);
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
private static extern short GetKeyState(int vKey);
[DllImport("user32")]
private static extern int GetKeyboardState(byte[] pbKeyState);
#endregion
[StructLayout(LayoutKind.Sequential)]
private class KeyboardHookStruct
{
public int vkCode;
public int scanCode;
public int flags;
public int time;
public int dwExtraInfo;
}
public Hook(HookType H, HookVisibility H2)
{
m_HookType = H;
m_Visibility = H2;
}
public bool Start()
{
if (m_HookType == HookType.KeyBoard)
m_Proc = new HookProc(KeyProc);
if (m_Visibility == HookVisibility.Global)
m_Hook = SetWindowsHookEx(getHookType(m_HookType, m_Visibility), m_Proc, GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName), 0);
else if (m_Visibility == HookVisibility.Local)
m_Hook = SetWindowsHookEx(getHookType(m_HookType, m_Visibility), m_Proc, GetModuleHandle((IntPtr)0), (int)GetCurrentThreadId());
if (m_Hook == (IntPtr)0)
return false;
return true;
}
public bool Stop()
{
return UnhookWindowsHookEx(m_Hook);
}
private int getHookType(HookType H, HookVisibility V)
{
if (H == HookType.KeyBoard && V == HookVisibility.Local)
return 2;
if (H == HookType.KeyBoard && V == HookVisibility.Global)
return 13;
else return -1;
}
private int getKeyBoardEventType(KeyBoardEventType K)
{
if (K == KeyBoardEventType.KeyDown)
return 0x100;
if (K == KeyBoardEventType.KeyUp)
return 0x101;
if (K == KeyBoardEventType.SysKeyDown)
return 0x104;
if (K == KeyBoardEventType.SysKeyUp)
return 0x105;
if (K == KeyBoardEventType.KeyShift)
return 0x10;
if (K == KeyBoardEventType.KeyCapital)
return 0x14;
if (K == KeyBoardEventType.NumLock)
return 0x90;
else return -1;
}
private IntPtr KeyProc(int nCode, int wParam, IntPtr lParam)
{
bool handled = false;
if ((nCode >= 0) && (m_onKeyDown != null || m_onKeyUp != null || m_onKeyPress != null))
{
KeyboardHookStruct MyKeyboardHookStruct = (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct));
if (m_onKeyDown != null && (wParam == 0x100 || wParam == 0x104))
{
Keys keyData = (Keys)MyKeyboardHookStruct.vkCode;
KeyEventArgs e = new KeyEventArgs(keyData);
m_onKeyDown(this, e);
handled = handled || e.Handled;
}
if (m_onKeyPress != null && wParam == 0x100)
{
bool isShift = ((GetKeyState(0x10) & 0x80) == 0x80 ? true : false);
bool isCapslock = (GetKeyState(0x14) != 0 ? true : false);
byte[] keyState = new byte[256];
GetKeyboardState(keyState);
byte[] inBuffer = new byte[2];
if (ToAscii(MyKeyboardHookStruct.vkCode, MyKeyboardHookStruct.scanCode, keyState, inBuffer, MyKeyboardHookStruct.flags) == 1)
{
char key = (char)inBuffer[0];
if ((isCapslock ^ isShift) && Char.IsLetter(key))
key = Char.ToUpper(key);
KeyPressEventArgs e = new KeyPressEventArgs(key);
m_onKeyPress(this, e);
handled = handled || e.Handled;
}
}
if (m_onKeyUp != null && (wParam == 0x101 || wParam == 0x105))
{
Keys keyData = (Keys)MyKeyboardHookStruct.vkCode;
KeyEventArgs e = new KeyEventArgs(keyData);
m_onKeyUp(this, e);
handled = handled || e.Handled;
}
}
if (handled)
return (IntPtr)1;
else
return CallNextHookEx(m_Hook, nCode, (IntPtr)wParam, (IntPtr)lParam);
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml.Linq;
使用PowerPoint=Microsoft.Office.Interop.PowerPoint;
使用Office=Microsoft.Office.Core;
使用System.Windows.Forms;
使用System.Runtime.InteropServices;
使用系统诊断;
命名空间测试挂钩
{
公共部分类ThisAddIn
{
吊钩;
私有void ThisAddIn_启动(对象发送方,System.EventArgs e)
{
hook=newhook(hook.HookType.KeyBoard,hook.HookVisibility.Global);
hook.OnKeyDown+=新的KeyEventHandler(hook\u OnKeyDown);
hook.Start();
}
void hook_OnKeyDown(对象发送器,KeyEventArgs e)
{
Show(例如KeyCode.ToString());
}
私有void ThisAddIn_关闭(对象发送方,System.EventArgs e)
{
hook.Stop();
hook=null;
}
#区域VSTO生成的代码
///
///设计器支持所需的方法-不修改
///此方法的内容与代码编辑器一起使用。
///
私有void InternalStartup()
{
this.Startup+=new System.EventHandler(ThisAddIn\u启动);
this.Shutdown+=new System.EventHandler(ThisAddIn\u Shutdown);
}
#端区
阶级挂钩
{
私有IntPtr m_Hook=(IntPtr)0;
私家车能见度mu能见度;
私有HookType m_HookType;
私有HookProc mu Proc;
公共枚举类型{键盘};
public enum KeyBoardEventType{KeyDown,KeyUp,SysKeyDown,syskeydup,KeyShift,KeyCapital,NumLock};
公共枚举{全局,局部};
私有委托IntPtr HookProc(intncode、intwparam、IntPtr lParam);
私钥事件处理程序m_onKeyPress;
私钥事件处理程序m_onKeyUp;
私钥事件处理程序m_onKeyDown;
公共事件KeyPress事件处理程序OnKeyPress
{
添加
{
m_onKeyPress+=值;
}
去除
{
m_onKeyPress-=值;
}
}
公共事件KeyEventHandler OnKeyUp
{
添加
{
m_onKeyUp+=值;
}
去除
{
m_onKeyUp-=值;
}
}
公共事件KeyEventHandler OnKeyDown
{
添加
{
m_onKeyDown+=值;
}
去除
{
m_onKeyDown-=值;
}
}
#德林波特地区
[DllImport(“user32.dll”,CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall,SetLastError=true)]
私有静态外部IntPtr setWindowshookx(intidhook、HookProc lpfn、IntPtr hmod、intdwthreadid);
[DllImport(“user32.dll”)]
私有静态外部IntPtr CallNextHookEx(IntPtr hHook、intncode、IntPtr wParam、IntPtr lParam);
[DllImport(“user32.dll”,CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall,SetLastError=true)]
私有静态外部bool unhookwindowshookx(IntPtr hHook);
[DllImport(“Kernel32.dll”,SetLastError=true)]
私有静态外部IntPtr GetModuleHandle(IntPtr lpModuleName);
[DllImport(“Kernel32.dll”,SetLastError=true)]
私有静态外部IntPtr GetModuleHandle(字符串lpModuleName);
[DllImport(“Kernel32.dll”)]
私有静态外部IntPtr GetCurrentThreadId();
[DllImport(“user32”)]
私有静态外部inttoascii(intuvirtkey,intuscancode,字节[]lpbKeyState,字节[]lpwTransKey,intfustate);
[DllImport(“user32.dll”,CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall)]
私有静态外部短GetKeyState(int vKey);
[DllImport(“user32”)]
私有静态外部int GetKeyboardState(字节[]pbKeyState);
#端区
[StructLayout(LayoutKind.Sequential)]
私有类KeyboardHookStruct
{
公共int-vkCode;
公共int扫描码;
公共国旗;
公共整数时间;
公共信息;
}
公共挂钩(挂钩类型H、挂钩类型H2)
{
m_HookType=H;
m_能见度=H2;
}
公共bool Start()
{
if(m_HookType==HookType.KeyBoard)
m_Proc=新的HookProc(KeyProc);
if(m_Visibility==HookVisibility.Global)
m_Hook=SetWindowsHookEx(getHookType(m_HookType,m_可见性),m_Proc,GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName),0);
else if(m_Visibility==HookVisibility.Local)
m_Hook=setWindowshookx(getHookType(m_HookType,m_可见性),m_Proc,GetModuleHandle((IntPtr)0),(int)GetCurrentThreadId());
如果(m_Hook==(IntPtr)0)
返回false;
返回true;
}
公共车站
{
返回Unhookwindowshookx(m_Hook);
}
私有int getHookType(HookType H,hookv)
{
if(H==HookType.KeyBoard&&V==HookVisibility.Local)
返回2;
if(H==HookType.KeyBoard&&V==HookVisibility.Global)
返回13;
else ret
private bool wParamAlt;
private IntPtr KeyProc(int nCode, int wParam, IntPtr lParam)
{
bool handled = false;
if ((nCode == 0) && (m_onKeyDown != null || m_onKeyUp != null || m_onKeyPress != null))
{
KeyboardHookStruct MyKeyboardHookStruct;
if (wParam >= 0x100)
{
MyKeyboardHookStruct = (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct));
wParamAlt = false;
}
else
{
MyKeyboardHookStruct = new KeyboardHookStruct();
MyKeyboardHookStruct.vkCode = wParam;
if (wParamAlt)
{
wParamAlt = (wParam == 18);
wParam = 0x104;
}
else
{
wParamAlt = (wParam == 18);
wParam = 0x100;
}
}
if (m_onKeyDown != null && (wParam == 0x100 || wParam == 0x104))
{
Keys keyData = (Keys)MyKeyboardHookStruct.vkCode;
if (wParam == 0x104)
keyData |= Keys.Alt;
KeyEventArgs e = new KeyEventArgs(keyData);
m_onKeyDown(this, e);
handled = handled || e.Handled;
}
if (m_onKeyPress != null && wParam == 0x100)
{
bool isShift = ((GetKeyState(0x10) & 0x80) == 0x80 ? true : false);
bool isCapslock = (GetKeyState(0x14) != 0 ? true : false);
byte[] keyState = new byte[256];
GetKeyboardState(keyState);
byte[] inBuffer = new byte[2];
if (ToAscii(MyKeyboardHookStruct.vkCode, MyKeyboardHookStruct.scanCode, keyState, inBuffer, MyKeyboardHookStruct.flags) == 1)
{
char key = (char)inBuffer[0];
if ((isCapslock ^ isShift) && Char.IsLetter(key))
key = Char.ToUpper(key);
KeyPressEventArgs e = new KeyPressEventArgs(key);
m_onKeyPress(this, e);
handled = handled || e.Handled;
}
}
if (m_onKeyUp != null && (wParam == 0x101 || wParam == 0x105))
{
Keys keyData = (Keys)MyKeyboardHookStruct.vkCode;
if (wParam == 0x105)
keyData |= Keys.Alt;
KeyEventArgs e = new KeyEventArgs(keyData);
m_onKeyUp(this, e);
handled = handled || e.Handled;
}
}
if (handled)
return (IntPtr)1;
else
return CallNextHookEx(m_Hook, nCode, (IntPtr)wParam, (IntPtr)lParam);
}