Quantcast
Channel: General Office Development forum
Viewing all articles
Browse latest Browse all 2257

Office Hook Key Shortcut

$
0
0

Hello,

I've an office add-in and uses a ribbon control. I have 2 buttons who can be called by shortcuts.

Ctrl-alt-o
Ctrl-alt-s

I created functionality with hook to catch the shortcut combinations and then call my functionality.

The problem is when I use Ctrl-alt-o the character o is displayed in my Word Document. Is there a way to prevent this to type this character?

This is my code:

using System;
using System.Diagnostics;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Microsoft.Office.Core;
using Microsoft.Office.Interop.Excel;
using Excel = Microsoft.Office.Interop.Excel;
namespace Hooking
{
    public class InterceptKeys
    {
        public delegate int LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
        private static LowLevelKeyboardProc _proc = HookCallback;
        private static IntPtr _hookID = IntPtr.Zero;
        private static Microsoft.Office.Tools.CustomTaskPane ctpRef = null;

        //Declare the mouse hook constant.
        //For other hook types, you can obtain these values from Winuser.h in the Microsoft SDK.            

        private const int WH_KEYBOARD = 2;
        private const int HC_ACTION = 0;

        public static void SetHook()
        {
            _hookID = SetWindowsHookEx(WH_KEYBOARD, _proc, IntPtr.Zero, (uint)AppDomain.GetCurrentThreadId());
        }

        public static void ReleaseHook()
        {
            UnhookWindowsHookEx(_hookID);
        }

        private static int HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (nCode < 0)
            {
                return (int)CallNextHookEx(_hookID, nCode, wParam, lParam);
            }
            else
            {
                if (nCode == HC_ACTION)
                {
                    Keys keyData = (Keys)wParam;
                    if (Functions.IsKeyDown(Keys.ControlKey) && Functions.IsKeyDown(Keys.ShiftKey) && keyData == Keys.O)
                    {
                        Functions.ShowOpenform();

                    }
                    if (Functions.IsKeyDown(Keys.ControlKey) && Functions.IsKeyDown(Keys.ShiftKey) && keyData == Keys.S)
                    {

                        Functions.ShowSaveform();
                    }
                }
                return (int)CallNextHookEx(_hookID, nCode, wParam, lParam);
            }
        }

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr SetWindowsHookEx(int idHook,
            LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool UnhookWindowsHookEx(IntPtr hhk);

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode,
            IntPtr wParam, IntPtr lParam);
    }

    public class Functions
    {
        public static bool IsKeyDown(Keys keys)
        {
            return (GetKeyState((int)keys) & 0x8000) == 0x8000;
        }

        public static void ShowOpenform()
        {
            _Excell_Add_in.OpenForm frmOpenForm = null;
            if ((frmOpenForm = (_Excell_Add_in.OpenForm)IsFormAlreadyOpen(typeof(_Excell_Add_in.OpenForm))) == null)
            {
                _Excell_Add_in.Splash frmSplash = new _Excell_Add_in.Splash();
                frmSplash.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
                //frmSplash.Opacity = 0.83;
                frmSplash.TransparencyKey = frmSplash.BackColor;
                //frmSplash.UpdateText("Instellingen controleren");
                frmSplash.Show();
                frmSplash.Update();
                _Excell_Add_in.Class1 clClass = new _Excell_Add_in.Class1();
                clClass.InitLib(ref _Excell_Add_in.Globals.ThisAddIn.Application);
                frmSplash.Hide();
                clClass.ShowForm();
            }
            else
            {
                frmOpenForm.Select();
            }
        }

        public static void ShowSaveform()
        {
            _Excell_Add_in.SaveForm frmSaveForm = null;
            if ((frmSaveForm = (_Excell_Add_in.SaveForm)IsFormAlreadyOpen(typeof(_Excell_Add_in.SaveForm))) == null)
            {
                if (_Excell_Add_in.Globals.ThisAddIn.Application.ActiveWorkbook == null)
                {
                    System.Windows.Forms.MessageBox.Show("Er is geen werkmap actief. Gelieve eerst een werkmap aan te maken.", "Geen werkmap geselecteerd", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
                }
                else
                {

                    _Excell_Add_in.Splash frmSplash = new Smartschool_Excell_Add_in.Splash();
                    frmSplash.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
                    //frmSplash.Opacity = 0.83;
                    frmSplash.TransparencyKey = frmSplash.BackColor;
                    //frmSplash.UpdateText("Instellingen controleren");
                    frmSplash.Show();
                    frmSplash.Update();

                    _Excell_Add_in.SaveForm frmBrowse = new Smartschool_Excell_Add_in.SaveForm(true);
                    frmSplash.Hide();
                    frmBrowse.ShowDialog();
                }
            }
            else
            {
                frmSaveForm.Select();
            }
        }

        public static Form IsFormAlreadyOpen(Type FormType)
        {
            foreach (Form OpenForm in System.Windows.Forms.Application.OpenForms)
            {
                if (OpenForm.GetType() == FormType)
                    return OpenForm;
            }

            return null;
        }

        [DllImport("user32.dll")]
        static extern short GetKeyState(int nVirtKey);

    }
}


Viewing all articles
Browse latest Browse all 2257

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>