Welcome to AddressOf.com Sign in | Join | Help

Windows Form application in Kiosk mode...

Someone asked how to make sure their application has focus and how to protect against it losing focus while running.  There are actually many applications that could require this sort of capability, one of which would be a kiosk.  Many years ago, I used somewhat of a hack to accomplish this task.  It was very simple and worked very well.  Just replace Explorer.exe as the shell with your application.  There were a few other tricks you could do trick Windows 9x into thinking the screen saver was running so you could prevent the task manager from being displayed.  I did a little checking and the shell replacement trick can still be done today.  You can modify the following registry setting:

[HKEY_LOCAL_MACHINE]\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\Shell = "sol.exe"

This would replace Explorer.exe with sol.exe... now that would be useful. ;-)  Now this trick would work for many scenarios that require the user to only run a single application on the machine, but what if you only want this to happen on a more temporary basis.  Surely there must be some way to do this.

There are probably a few ways to accomplish this and I've put together an example showing at least one way of doing so.  The sample is designed to run as a full screen application and has a single button so you can exit the program.  It has no border and starts up maximized.  By setting the form this way, it will fill the screen.  Next, I use SetWindowsPos to force the window to stay in the foreground (except for the shell, which has priority).  Next, I hook into the the lower level portions of windows to trap keyboard handling prior to other applications being able to see them.  If I see a key combination I want to suppress, I can just swallow the event and all the other applications would be none the wiser.

I've done a bit of research using Google to gather some of this information, enhanced and transformed it a bit and now have what I believe is a much better implementation of this than any of the other examples I could find.  For example, you don't necessarily have to call upon Enable to re-enable the keys... the class implements IDisposable; so if you forget or would like to use Dispose, your still safe.

[disclaimer] I haven't really done a whole lot of testing with this code, so venture forth accordingly.

Download the code.

Published Wednesday, April 06, 2005 1:22 AM by CorySmith
Filed under: , ,

Comments

# re: Windows Form application in Kiosk mode...

Thursday, January 05, 2006 5:35 PM by Kaalinashi
Awesome, I've been looking for exactly this.

# re: Windows Form application in Kiosk mode...

Thursday, February 02, 2006 8:20 AM by Gagan
hey it doesnt handle ALT+CTRL+DEL

is there any other way rather than disabling the task manager through registry to not allow the task manager to come up



# re: Windows Form application in Kiosk mode...

Monday, March 13, 2006 7:12 AM by Marc
Great peace of code, but it doesn't work in VB.NET 2005, any suggestions?

# re: Windows Form application in Kiosk mode...

Monday, May 29, 2006 8:05 AM by steve
Thanks, just what I needed.

# re: Windows Form application in Kiosk mode...

Monday, July 17, 2006 11:05 AM by Ava
HOw do you get it to work with VB.NET 2005 and .Net 2.0?

# re: Windows Form application in Kiosk mode...

Monday, July 17, 2006 8:15 PM by Cory Smith
The code does appear to work as expected from VB 2005 (and .NET 2.0). However, please keep in mind that during development (inside of Visual Studio) and debugging the application, Visual Studio is hooking the keyboard prior to your application to allow for debugging. Build the application, in release mode, and run the application seperately of Visual Studio. At this point, you should get the intended results.

# re: Windows Form application in Kiosk mode...

Monday, July 17, 2006 8:20 PM by Cory Smith
Also, with further testing, it appears that you could use Me.TopMost = True instead of the SetWindowPos API in VB 2005 inside the Form1_Load event.

# re: Windows Form application in Kiosk mode...

Wednesday, July 19, 2006 12:22 PM by Ava
Thanks, Cory! You are very right!

# re: Windows Form application in Kiosk mode...

Thursday, July 20, 2006 12:01 PM by Ava
New Problem: Doesn't seem to be a very stable solution. Try running the .exe and hitting the TAB key repeatedly, similar to what you would do to tab through controls. Also try hitting just the ALT key repeatedly. It crashes.

# re: Windows Form application in Kiosk mode...

Monday, December 18, 2006 4:50 PM by Justin

@Ava: I can't reproduce that problem.  Works fine.

@Cory: Thanks for this!  First Google result and this is exactly what I was after.

# re: Windows Form application in Kiosk mode...

Friday, September 14, 2007 5:34 AM by Plater
I would bet that the "hitting the tab key multiple times" and the ALT key are related to the "sticky keys" setup for accessability options. If you turn them ALL off (there's like a half-dozen of them) it might be more stable?
Anonymous comments are disabled