PPT VSTO: how to excute shortcut over office's internal command

309 Views Asked by At

I was lucky to find one project called globalmousekeyhook, by which I can set a shortcut to my PPT VSTO addin.
The core part code is as below.
When I pressed the defined shortcut, both my addin's function and PPT's internal command will be executed.
This has one side effect that the window triggered by my addin won't be focused.
I've tried to add frm.activate() or frm.focus, but none of them works.
What can I do to excute only my addin's function? Any comments will be apprciated.

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Gma.System.MouseKeyHook;
namespace PowerPointAddIn1
{
    public partial class ThisAddIn
    {
        public static PowerPoint.Application PPTApp;
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        { 
            var QRun = Combination.FromString("Alt+E");
            Action actionQRun = ShowQRunWin;
            var assignment = new Dictionary<Combination, Action>
            {
                {QRun, actionQRun}
            };
            Hook.AppEvents().OnCombination(assignment);
        }

        public void ShowQRunWin()
        {            
            CMDForm frm = new CMDForm();
            frm.FormBorderStyle = FormBorderStyle.FixedSingle;   //set it un-resizeable       
            frm.MaximizeBox = false;  //remove maximize button
            frm.MinimizeBox = false;  //remove minimize button                
            frm.Show();
            frm.Activate();
            frm.Focus();           
        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
        }

        #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
        protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject()
        {
            return new RibbonUI();
        }
    }
}
1

There are 1 best solutions below

2
On

The project you are referring to allows canceling any further actions by setting the KeyPressEventArgs.Handled property which sets a value indicating whether the event was handled - true to bypass the control's default handling; otherwise, false to also pass the event along to the default control handler.

Another approach is to repurpose a corresponding (if any) ribbon button. In that case keyboard shortcuts are covered as well. Read more about that in the Temporarily Repurpose Commands on the Office Fluent Ribbon article.