Link to home
Start Free TrialLog in
Avatar of BlearyEye
BlearyEyeFlag for United States of America

asked on

Suppress alt-tab when app is running

I'm using C# 4.0. I want to suppress alt-tab while a certain application is running and enable it when it's not. The re-enablement should happen when the app stops for any reason, including a crash.

IMessaageFilter doesn't seem to be the right way to do it since, as I understand it, alt-tab is Windows level and isn't passed in to the app.

I guess one way to do it would be to set a system-wide hook to suppress alt-tab. Then I could launch a separate process that checks periodically to see whether my app is running or not; if not, it would re-enable alt-tab and then exit.

But this seems really complicated. Is there an easier way?
SOLUTION
Avatar of abdkhlaif
abdkhlaif
Flag of Saudi Arabia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of BlearyEye

ASKER

ok. I'll wait around for a while to see if anyone else weighs in.
Hi,

I think the Keyboard hook IS the best option.
I have already written some good code for keyboard hooks, and you don't need any additional hook DLLs or anything -- just pure C# (managed and Pinvoke).

I can put a sample C# application together if you want, so you can see how easy it is.
ricovox: if you don't mind sending me your code, I'll compare it to the link that abdkhlaif sent ...
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
BTW, I should point out that it is important to follow standard conventions when using such powerful techniques as global keyboard hooks. Please don't disable Alt+Tab or alter standard processes unless you have extreme circumstances (such as you are developing a POS system etc). Otherwise your users may get very mad that you are not using proper programming "etiquette".
Thanks. I'll give it a try (on a test machine). It looks like it has everything I need to set and unset the hook. With that I can write a process that checks to see if my application is running, setting or unsetting the hook as appropriate.

The application, btw, is being run in a kiosk-like environment so they don't expect normal PC behavior.
Hmmm ... when I click Install Hook, I get
  Win32Exception was unhandled: The parameter is incorrect
in method Hook() ...

hmm. that is strange.
Can you tell me more about your OS? What version? 32bit or 64bit?

Thanks
And what version of VS are you using?
Vista 32 bit
Visual Studio 2010
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hmm ... same problem this time.
Sorry, BlearyEye. I was hoping those corrections would help clear up the problem.

Apparently there is something wrong with the call to SetWindowsHookEx.
I will try to look into this further, but unfortunately I can't duplicate the problem you are having (even when I run in x86 mode).
I will try on a WinXP 32 bit machine later tonight.

Can you do some troubleshooting for me? See if any of the following work

1) Try to run the exe from the bin folder (outside of visual studio)

2) Try to run the exe AS ADMINISTRATOR (probably UAC is causing problems)

3) Place a breakpoint on the line that says

IntPtr hInstance = Marshal.GetHINSTANCE(typeof(KeyboardHook).Module);

Make sure that when the code goes through that line that hInstance has a value other than 0.

Then make sure that after the call to "SetWindowsHookEx", that hHook does equal 0 (just to make sure that the error is in that line).


Thanks!
Running .exe directly (outside of VS) works. How odd ...

I am running VS as admin. I've also disabled UAC.

Running from VS and setting checkpoint, hInstance = 26083328 and it bombs

Running .exe directly, attaching VS to process, hInstance = 2949120 and it succeeds
well I'm not surprised the hInstance is different. That will probably be different each time you run the application.

But it IS surprising that it works outside of VS but not inside. Maybe something to do with the VS2010 host. Like I said it works fine in VS2008 for me.

Anyway, are you pleased with the ability of the program to suppress Alt+Tab?

You mentioned before that you would need to write a separate program to detect when your main program is running and install the keyboard hook when it is running. But if your main program is also written in C# (or another .net language), then why don't you just put the keyboard hook logic in it and simply install the keyboard hook on startup and unhook it on close? Obviously you should do whatever you feel is best for your application. I just wanted to point out that there is no restriction in terms of which application installs the keyboard hook. It can be in your main application or wherever you want.

BTW, if the fact that it crashes during hosting is a major problem, perhaps you can ask a question about why that might happen (in a separate "question", since it is somewhat unrelated to your original question).
Yes, it works fine, thanks.
Note that ricovox sent two versions of KeyboardHook class. Be sure to use the second one.
btw, the reason I want this to run as a separate process is that the application can bomb. In that case, I want alt-tab to be automatically restored. If I do it in the application, alt-tab will be left suppressed.