none
excel2013:disable ctrl+x and ctrl+c using vsto RRS feed

  • Question

  • excel2013:I want to disable ctrl+x and ctrl+c using vsto.

    code

    this.Application.OnKey("^c", "");

    this code can not disable ctrl+c.

    How to deal with it??



    忘忧草,我的博客http://xiangzhangjun2006.blog.163.com

    Monday, November 20, 2017 10:53 AM

Answers

  • Hello,

    You could use

       class KeyboardHooking
        {
            [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);
    
            [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
            private static extern IntPtr GetModuleHandle(string lpModuleName);
    
            public delegate int LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
            private static LowLevelKeyboardProc _proc = HookCallback;
            private static IntPtr _hookID = IntPtr.Zero;
    
            //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; // mouse
            private const int HC_ACTION = 0;
    
            private const int WH_KEYBOARD_LL = 13; // keyboard
            private const int WM_KEYDOWN = 0x0100;
    
            public static void SetHook()
            {
                // Ignore this compiler warning, as SetWindowsHookEx doesn't work with ManagedThreadId
    #pragma warning disable 618
                _hookID = SetWindowsHookEx(WH_KEYBOARD, _proc, IntPtr.Zero, (uint)AppDomain.GetCurrentThreadId());
    #pragma warning restore 618
    
            }
    
            public static void ReleaseHook()
            {
                UnhookWindowsHookEx(_hookID);
            }
    
            //Note that the custom code goes in this method the rest of the class stays the same.
            //It will trap if BOTH keys are pressed down.
            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;
    
                        // CTRL + X
                        if ((BindingFunctions.IsKeyDown(Keys.ControlKey) == true)
                            && (BindingFunctions.IsKeyDown(keyData) == true) && (keyData == Keys.X))
                        {
                            MessageBox.Show("Disabled");
                            return -1;
                        }
    
                        // CTRL + C
                        if ((BindingFunctions.IsKeyDown(Keys.ControlKey) == true)
                            && (BindingFunctions.IsKeyDown(keyData) == true) && (keyData == Keys.C))
                        {
                            MessageBox.Show("Disabled");
                            return -1;
                        }
    
                        // CTRL + V
                        if ((BindingFunctions.IsKeyDown(Keys.ControlKey) == true)
                            && (BindingFunctions.IsKeyDown(keyData) == true) && (keyData == Keys.V))
                        {
                            MessageBox.Show("Disabled");
                            return -1;
                        }
    
    
                    }
                    return (int)CallNextHookEx(_hookID, nCode, wParam, lParam);
                }
            }
        }
    
        public class BindingFunctions
        {
            [DllImport("user32.dll")]
            static extern short GetKeyState(int nVirtKey);
    
            public static bool IsKeyDown(Keys keys)
            {
                return (GetKeyState((int)keys) & 0x8000) == 0x8000;
            }
    
        }
    
    
     private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
                KeyboardHooking.SetHook();
            }
    
            private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
            {
                KeyboardHooking.ReleaseHook();
            }
    
    

    Reference:

    How to perform .Onkey Event in an Excel Add-In created with Visual Studio 2010?

    Besides, i suggest you mark helpful post or your post as answer to close your previous thread:

    https://social.msdn.microsoft.com/Forums/en-US/5e2d8916-7478-4c92-92ab-7b06d197328c/word2013ppt2013excel2013i-want-to-disable-the-saveas-button-according-to-the-path?forum=officegeneral

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by LOVE007 Wednesday, November 22, 2017 6:57 AM
    • Unmarked as answer by LOVE007 Wednesday, November 22, 2017 6:57 AM
    • Marked as answer by LOVE007 Wednesday, November 22, 2017 6:57 AM
    Tuesday, November 21, 2017 6:19 AM
    Moderator