none
How can I change color of cell, when "Some key" is pressed? (Excel Add in) RRS feed

  • Question

  • So far my result is, that the color of my "A1" cell - change it self, but only when Excel is in background or inactive. 

    Is it possible to do when Excel is active? So I can press some key, or combination of keys, and my "A1" cell would change color...

            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);
            private static HookProcedure procedure = HookCallback;
    
            [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(procedure);           
            }
    
           
    
            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 static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
            {
                if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
                {
                    int pointerCode = Marshal.ReadInt32(lParam);
                    string pressedKey = ((Keys)pointerCode).ToString();
    
        
                    var thread = new Thread(() => {
    
                        Excel.Range rng2 = ExcelAddIn1.Globals.ThisAddIn.Application.get_Range("A1");
                        rng2.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
    
                    });
                    thread.Start();
                }
                return CallNextHookEx(hookId, nCode, wParam, lParam);
            }

    • Moved by CoolDadTx Tuesday, January 19, 2016 2:57 PM Office related
    Tuesday, January 19, 2016 11:21 AM

Answers