I am working on a VSTO Word AddIn project and I copy some paragraph from an external source (some other word doc) and want to paste it by using Ctrl V in word addin. I want to add some more custom functionalities along with the paste operation. how to do that ?
CodePudding user response:
To control the keyboard shortcuts for pasting some data into your document you can set up a keyboard hook using the SetWindowsHookEx
Windows API function. For example:
private const int WH_KEYBOARD_LL = 13;
private const int WM_KEYDOWN = 0x0100;
private static IntPtr hookId = IntPtr.Zero;
private delegate IntPtr HookProcedure(int nCode, IntPtr wParam, IntPtr lParam);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr GetModuleHandle(string lpModuleName);
[DllImport("user32.dll", SetLastError = true)]
private static extern bool UnhookWindowsHookEx(IntPtr hhk);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr SetWindowsHookEx(int idHook, HookProcedure lpfn, IntPtr hMod, uint dwThreadId);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
hookId = SetHook(HookCallback);
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
UnhookWindowsHookEx(hookId);
}
private static IntPtr SetHook(HookProcedure procedure)
{
using (Process process = Process.GetCurrentProcess())
using (ProcessModule module = process.MainModule)
return SetWindowsHookEx(WH_KEYBOARD_LL, procedure, GetModuleHandle(module.ModuleName), 0);
}
private IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
try
{
if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
{
int pointerCode = Marshal.ReadInt32(lParam);
string pressedKey = ((Keys)pointerCode).ToString();
//Do some sort of processing on key press
var thread = new Thread(() =>
{
if (Control.ModifierKeys != 0 && pointerCode == 48 && Keys.Shift != 0)
{
//
Microsoft.Office.Interop.Word.Selection currentSelection = Application.Selection;
if (currentSelection.Type == Word.WdSelectionType.wdSelectionIP)
{
currentSelection.TypeBackspace();
currentSelection.TypeText("()");
currentSelection.MoveLeft(1);
pointerCode = 0;
}
else
if (currentSelection.Type == Word.WdSelectionType.wdSelectionNormal)
{
currentSelection.TypeBackspace();
currentSelection.MoveLeft(1);
currentSelection.TypeText("()");
pointerCode = 0;
}
else
{
// Do nothing.
}
}
else
{
}
});
thread.Start();
}
}
catch
{
}
return CallNextHookEx(hookId, nCode, wParam, lParam);
}
To handle ribbon controls (including context menus) you can repurpose built-in controls. See Temporarily Repurpose Commands on the Office Fluent Ribbon for more information.
CodePudding user response:
You can patch the GetClipboardData Windows API function using Detours and run any code you want in your new function.
Works for both Ctrl V and paste from a popup menu.