- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我遇到了一个问题,无法让我的 Office 插件在 Powerpoint 2013 上与我的全局键盘一起工作,但在以前的版本(2007 和 2010)上却没有。
我没有收到任何异常,但似乎从未在 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);
}
}
}
我的应用程序需要在幻灯片放映期间触发事件,因为我有一些其他窗口在演示期间显示,我必须根据用户按下的键更新它们。我尝试了很多解决方案,但钩子(Hook)是唯一可以完美完成工作的解决方案。
我也尝试过使用本地键盘钩子(Hook)而不是全局键盘钩子(Hook)。实际上,我认为这是让它工作的唯一方法,因为它是来自 Microsoft 的错误,而不是来自代码的错误。但是,我无法使本地的在任何版本的 Powerpoint 上正常工作。
最佳答案
该问题不是 Powerpoint 特有的,任何 Office 产品都会出现。我正在处理 Outlook 插件并遇到这个问题。已向 Microsoft 报告(不幸的是没有任何答复): https://social.msdn.microsoft.com/Forums/office/en-US/93d08ccc-9e77-4f72-9c51-477468d89681/keyboardhook-will-not-work-in-word-2013?forum=worddev
我已经能够做出“解决方法”:我已经注册了一个全局 Hook 和一个本地 Hook (“线程”)。它有效,但 keyProc 在本地时被称为“奇怪的”,不考虑记录的参数: - wParam 不是 WM_* 之一,而是直接包含 vkCode - 无法访问 lParam (AccessViolation)据我了解,这是因为一些“TranslateMessage”处理。
我还调整了您的代码以能够捕获 ALT 组合。
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);
}
关于Office Addin 2013 中的 C# 全局键盘 Hook ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26401953/
我创建了一个简单的钩子(Hook),我安装了它 SetWindowsHookEx(WH_CBT, addr, dll, 0); 完成后,我卸载 UnhookWindowsHookEx(0); 然后我可
我正在使用 React Hooks,当我用 mobx 的观察者包装我的组件时,我收到了这个错误。可能是什么问题?是否可以将 mobx 与 React Hooks 一起使用? import classn
我知道这个问题已经被回答过很多次了。我只是找不到解决我的问题的答案,让我相信,我要么是愚蠢的,要么是我的问题没有被解决,因为它比我更愚蠢。除此之外,这是我的问题: 我正在尝试创建一个功能组件,它从 r
我正在使用 React Navigation 的 useNavigation 钩子(Hook): 在 MyComponent.js 中: import { useNavigation } from "
我想在 gitlab 中使用预提交钩子(Hook)。我做的一切都像文档中一样:https://docs.gitlab.com/ce/administration/custom_hooks.html 在
我最近在和一些人谈论我正在编写的程序时听到了“hook”这个词。尽管我从对话中推断出钩子(Hook)是一种函数,但我不确定这个术语到底意味着什么。我搜索了定义,但找不到好的答案。有人可以让我了解这个术
我正在寻找一个在页面创建或页面更改后调用的钩子(Hook),例如“在导航中隐藏页面”、“停用页面”或“移动/删除页面“ 有人知道吗? 谢谢! 最佳答案 这些 Hook 位于 t3lib/class.t
我正在使用钩子(Hook)将新方法添加到 CalEventLocalServiceImpl 中... 我的代码是.. public class MyCalendarLocalServiceImpl e
编译器将所有 SCSS 文件编译为 STANDALONE(无 Rails)项目中的 CSS 后,我需要一个 Compass Hook 。 除了编辑“compiler.rb”(这不是好的解决方案,因为
我“.get”一个请求并像这样处理响应: resp = requests.get('url') resp = resp.text .. # do stuff with resp 阅读包的文档后,我看到
我们想在外部数据库中存储一些关于提交的元信息。在克隆或 checkout 期间,应引用此数据库,我们将元信息复制到克隆的存储库中的文件中。需要数据库而不是仅仅使用文件是为了索引和搜索等...... 我
我有一个 react 钩子(Hook)useDbReadTable,用于从接受tablename和query初始数据的数据库读取数据。它返回一个对象,除了数据库中的数据之外,还包含 isLoading
在下面的代码中,当我调用 _toggleSearch 时,我同时更新 2 个钩子(Hook)。 toggleSearchIsVisible 是一个简单的 bool 值,但是,setActiveFilt
问题 我想在可由用户添加的表单中实现输入字段的键/值对。 参见 animated gif on dynamic fields . 此外,我想在用户提交表单并再次显示页面时显示保存的数据。 参见 ani
当状态处于 Hook 状态时,它可能会变得陈旧并泄漏内存: function App() { const [greeting, setGreeting] = useState("hello");
const shouldHide = useHideOnScroll(); return shouldHide ? null : something useHideOnScroll 行为应该返回更新后
我正在使用 React-native,在其中,我有一个名为 useUser 的自定义 Hook,它使用 Auth.getUserInfro 方法从 AWS Amplify 获取用户信息,并且然后获取返
我正在添加一个 gitolite 更新 Hook 作为 VREF,并且想知道是否有办法将它应用于除 gitolite-admin 之外的所有存储库。 有一个更简单的方法而不是列出我想要应用 Hook
如何使用带有 react-apollo-hooks 的 2 个 graphql 查询,其中第二个查询取决于从第一个查询中检索到的参数? 我尝试使用如下所示的 2 个查询: const [o, setO
我是 hooks 的新手,到目前为止印象还不错,但是,如果我尝试在函数内部使用 hooks,它似乎会提示(无效的 hook 调用。Hooks can only be called inside o
我是一名优秀的程序员,十分优秀!