Windows keyboard hook hangs debugger - windows

I have a plug-in DLL that is launched from a secondary (not GUI) thread in the main application. The DLL must grab keyboard events from the application's main window. I am using SetWindowsHookEx() and it works great.
However, if I hit a breakpoint in Visual Studio while the hook is active, and then press a key, Visual Studio and my application lock up completely. This is a problem because I usually use the F-keys to step through code. If I use the mouse and step forward from Visual Studio's Debug menu, it works fine. But using F10 et al. is such a habit, I usually forget to use the mouse.
Is there anything I can do to get my keyboard shortcuts back? Or a different method to listen to the keyboard?

That's pretty inevitable, the debugger break stops the hook from handling notifications so the keyboard goes dead. Using the remote debugger on another machine is a good way to debug code like this. You'll also want to increase the hook timeout so Windows doesn't destroy the hook while you are debugging, HKEY_CURRENT_USER\Control Panel\Desktop\LowLevelHooksTimeout setting. Assuming you are using WH_KEYBOARD_LL.

Related

Can I force a windows 10/11 program to be unresponsive?

Is there anyway to set a program to be unresponsive in windows?
I have python code that starts a program. But when the program becomes unresponsive i want to do some specific clean up task.
But how do I test it. Because currently it is random.
Is there a way to either make a small program that I can force to be unresponsive?
Or force a running program to be unresponsive.
An "unresponsive" program is typically a GUI application that's blocked on it's main (UI) thread such that it hasn't pumped messages in a long time. That is, the UI feels "frozen" when you try to interact with it and/or the window doesn't seem to repaint itself as it should.
You can easily simulate this by creating a Windows Forms app in C# or Win32 GUI app in C++ using the default project templates in Visual Studio. Or basically any UI framework like Qt for that matter. Then introduce a very long sleep statement (several minutes) in response to a button click.
I don't think the "unresponsive" attribute applies to services, console applications, or background tasks.
Use Alt + Tab keyboard shortcut to bring your unresponsive program to the front, and then press Alt + F4 keys at the same time to force the program to quit.

Can I freeze the mouse location when entering break mode and restore it on returning to run mode?

Is there a facility in Visual Studio, when debugging a desktop app, to remember where the mouse was when I hit a break point so that i may use it in Visual Studio while stepping through code, and then snap it back to that spot when I resume execution? This would help in mouseover events and other position-sensitive workflows.
If there's not something built in, would it be possible to do this with a Visual Studio plugin? Or is there a way to install a hook such that the debugger sends my application an event or interrupt that it could use to save that state on break and restore it on resume?
Thank you.
There's nothing built in to the VS debugger to do this. It's more than mouse position, there are likely subtle state changes (like focus and activation) that would be difficult to save and restore reliably.
A quick and dirty thing to do is to add some OutputDebugString messages to the code in question to give you clues as to what's going on internally without actually breaking the execution. VS will show the messages in the Output window.
The only way to come anywhere close to what you're after is to use remote debugging. You can remote debug your code running inside a virtual machine so that you don't actually need a separate physical machine.

Step-through in VS without VS having focus

I find myself working with GUI code where the GUI program needs input focus and remain the topmost window, but whenever I'm debugging with VS stepping-through with F5/F10/F11 requires that VS has focus.
Is it possible to have VS intercept the F-keys whilst the debugee has focus? If VS doesn't have this functionality I imagine it should be possible to write a simple program or VS add-in that has a keyboard book and commands the debugger accordingly - has anyone developed such a program?
I'm working with a GUI test automation framework that sends mouse-clicks and other events by moving the cursor. When the debugee program is out of focus any click on its surface brings the main window forward but does not activate any controls, but the automation framework assumes that its focus of the application will never be interrupted. So if I set a breakpoint before a click that is meant to open the File menu then the click that is sent will only restore the debugee's focus and not open the File menu (if that makes sense).
I've done some searching but couldn't find anything immediately.
Why do you need to maintain focus? Have you specific hooks in the GotFocus/LostFocus?
I've had problems before with the Paint event being called as soon as F5 was hit causing the debugger to show again and therefore requiring another repaint. I got around these simply by arranging my windows so they didn't overlap. I'm pretty sure the LostFocus/GotFocus pair also don't fire when the windows are arranged this way too.

Program window not viewable during debug pause

When my program is paused in Visual Studios 2010 during debugging, like from reaching a break point and me doing a manual step through, the program window becomes impossible to view.
It is a GUI window not a console window, which I run simultaneously with my program and am still able to view. The window seems to be open it's just that when I click its icon on the taskbar it doesn't come to the front of all the other windows. When I minimize all the windows in front of it, I see the outline of the window but it is either blacked out or showing the remnants of previously expanded windows.
I've noticed this with using Visual Studio's before (various versions of it), and after trying other IDE's that didn't have this behavior I notice it more. It would be really helpful to view the program's change's as I step through the program. Anyone know how I can do this?
I searched a long while and couldn't find a single reference to this matter.
The reason the window doesn't display is that the window paint message won't be processed if the main thread has been paused. Which other IDEs let you do this? I haven't come across any native code debuggers that do this on Windows.
If you are stepping through code that is run by the main thread, then the main thread can't simultaniously poll the message pump, which is needed for the GUI to work.
If you debug a different thread, the GUI will work while you are debugging.

Visual Studio: How to stop breakpoint hit from stealing focus?

When a breakpoint is hit in Visual Studio, it steals the focus from whatever other application the programmer is viewing/typing into at that moment. This can be very irritating since VS grabs any keyboard input the programmer was typing into the other application at that moment and takes that input as its own.
What are the tricks you folks use to prevent this focus steal?
(I face this on Visual C++ 2008 and 2010. I am guessing it is a problem for Visual Studio in general and for all recent versions.)
This is finally fixed in VS2019. Go to Tools->Options->Debugging->General, down at the bottom is "Bring Visual Studio to the foreground when breaking in the debugger."
Just de-select it and you will no longer be interrupted while multitasking.
This is a registry setting. See ForegroundLockTimeout at http://technet.microsoft.com/en-us/library/cc957208.aspx. Zero allows applications to steal focus. TweakUI sets this value to 200000 when "Prevent applications from stealing focus" is checked.
For more control, download the Tweak UI utility of Powertoys for Windows XP. In the "General" tab, select "Focus" and check "Prevent applications from stealing focus".
Google search for ForegroundLockTimeout at http://www.google.com/search?q=ForegroundLockTimeout
Bing search for Prevent applications from stealing focus at http://www.bing.com/search?q=Prevent+applications+from+stealing+focus
Applications Stealing Focus on Windows XP at http://mycvs.org/archives/2004/11/16/applications-stealing-focus-on-windows-xp for screen capture of TweakUI.
Please Don't Steal My Focus, Coding Horror, Jeff Atwood at http://www.codinghorror.com/blog/2007/12/please-dont-steal-my-focus.html
The strange thing is, there are
provisions built into the operating
system to protect us from badly
written, focus stealing applications.
The ForegroundLockTimeout registry
setting is expressly designed to
prevent applications from stealing
focus from the user. The OS silently
converts that inappropriate focus
stealing behavior into friendlier,
less invasive taskbar button flashing,
which is the subject of the
ForegroundFlashCount registry setting.
How To Prevent Programs from Stealing Focus in Windows XP at http://www.howtodothings.com/computers-internet/how-to-prevent-programs-from-stealing-focus-in-windows-xp
Right click the breakpoint and select When hit ... this will allow you to run a function when the breakpoint is hit. You can use this to print status messages to the output window. You application will keep focus.
By accident I discovered a workaround, which I've been using for a few years now and while I haven't tested it in 2008 and 2010, it certainly works in 2013, '15 & '17 and at least Windows 7 & 10.
It relies on the fact that Visual Studio will not steal focus if another Visual Studio instance is paused in execution. Obviously the only thing as special as VS is another VS. :-/
Open a second instance of VS with a simple project. Pause the execution of the project anyway you like (e.g. put a breakpoint on the first line of execution and debug), you can then simply minimise that VS and none of the VS instances you're actually using will steal focus.
This is is obviously a heavy weight solution, but if you have ample RAM (the CPU usage of the idle VS doesn't even register for me), it works well. I haven't tried it with inter-version instances (e.g. pausing in '13 to make '17 behave), but if that works you'll probably want to opt to use the older version instance as your "dummy" VS to cut down on resource use.
One workaround is to use OutputDebugString() function to output current state into the debugger output window. You just place Visual Studio in background, position the debugged program window so that the "Output" window is visible - and no focus transition ever happens.
You will perhaps want to use macros for conditional compilation so that tracing code is not included into the release builds.

Resources