rubberduck-vba / Rubberduck

Every programmer needs a rubberduck. COM add-in for the VBA & VB6 IDE (VBE).
https://rubberduckvba.com
GNU General Public License v3.0
1.9k stars 298 forks source link

Hotkey For Refactor Sometimes Not Working #3996

Open spinjector opened 6 years ago

spinjector commented 6 years ago

Version 2.2.6693.19208 OS: Microsoft Windows NT 6.1.7601 Service Pack 1, x64 Host Product: 2007 Microsoft Office system x86 Host Version: 12.0.6735.5000 Host Executable: MSACCESS.EXE

version info VBA IDE: Microsoft Visual Basic 6.5 Version 1057 VBA: Retail 6.5.1057 Forms3: 12.0.6723.500 <--- other digit(s) possibly truncated by edge of dialog

I started using RD for the first time last week. I'm having a frequent issue where the hotkey for Refactor doesn't work. Details are as follows...

I'm in a code module by any means, such as Alt+Tab, opening an XLS or ACCDB and then a code module, or I'm already in VBA and double-click a module in Project Explorer. Then I work on the code. I press the hotkey for Code Explorer...and nothing happens. Sometimes if I Sync the Code Explorer it helps, but this is rare.

If I use the RD menu to access the Refactor dialog, this fixes the Hotkey for a while, but it always breaks again. That would be Menu> Rubberduck > Refactor > Rename, possibly a Resync first if it's grayed out. What's done on the dialog doesn't matter, it can either be canceled or used to rename something. Either way the result is the same; the Hotkey starts working. But only for a while; eventually it breaks again.

I don't know what the troubleshooting tricks for RD are, but here's what I've done:

  1. Disabled another add-in called PrettyCodePrint, the only other add-in which I use.
  2. Changed the Refactor hotkey to other values, such as Shift+Ctrl+Alt+X.
  3. Rebooted.
  4. Tried other XLS and ACCDB files. The issue occurs in both Excel and Access.

Note: I haven't yet tried to reinstall anything such as Office 2007, since this is my primary code/hacking machine and I would rather not have to recreate the environment.

spinjector commented 6 years ago

Some additional info to add. I changed the Hotkey character assignment back to its default Ctrl+Shift+R, but somewhere along the way my fingers got confused, and numerous times over the past hour I've hit Ctrl+Shift+T by accident...and that DOES work. But if I immediately press Ctrl+Shift+R, nothing happens. So it seems the "listener" for RD hotkeys is working...almost. It likes Ctrl+Shift+T, but not Ctrl+Shift+R. Also, Ctrl+Shift+` (Refresh) works too. I'd test some of the others, but I don't know what the other functions do yet. Still a Noob. =-)

retailcoder commented 6 years ago

The VBIDE API doesn't provide any infrastructure for hotkeys, so we (people building VBIDE add-ins) need to roll our own - we basically subclass the VBE's MainWindow and handle WM_Hotkey messages (which works because we register them with User32.RegisterHotkey), however because these messages are system-wide, we need to "detach" the hooks and "re-attach" them when the VBE loses/gets focus.

Whenever RD hotkeys aren't responding, a work-around would be to activate some code pane, or otherwise force the VBE to handle a SETFOCUS message:

    public override int SubClassProc(IntPtr hWnd, IntPtr msg, IntPtr wParam, IntPtr lParam, IntPtr uIdSubclass, IntPtr dwRefData)
    {
        var suppress = false;
        switch ((WM)msg)
        {
            case WM.HOTKEY:
                suppress = hWnd == Hwnd && HandleHotkeyMessage(wParam);
                break;
            case WM.SETFOCUS:
                Attach();
                break;
            case WM.RUBBERDUCK_CHILD_FOCUS:
                if (lParam == IntPtr.Zero)
                {
                    Detach();
                }
                else
                {
                    Attach();
                }
                suppress = true;
                break;
            case WM.NCACTIVATE:
                if (wParam == IntPtr.Zero)
                {
                    Detach();
                }
                break;
            ...

Not sure how to fix this reliably.

bclothier commented 6 years ago

Instead of fixing this, we can consider using a different approach suggested by @WaynePhillipsEA -- using SetWindowsHookEx API. That would eliminate the need to attach & detach on focus change and if we opt to hook to the low level keyboard state, that would allow for two step shortcuts.

Vogel612 commented 6 years ago

@bclothier if we don't have an issue open for that, I'd recommend moving that to a separate [technical-debt]/[enhancement] issue, to enable discussing that without derailing this issue

comintern commented 6 years ago

Linking chat, might be relevant.