Sample Code - Miscellaneous

These projects demonstrate some of the features found in the IndieLib. Most if not all were used for prototyping various aspects of game play during development. The IndieLib is a good third party library for quick prototyping. IndieLib uses DirectX 9.0 under the hood as well as a few other dependent libraries.

You do not need to install the IndieLib SDK or any of the other dependent libraries in order to compile and run these samples. But if you plan to get deeper into the library it is suggested that you do install IndieLib at least. You can find it here.

All projects that include source code were created using Microsoft Visual Studio 2005 and 2008 project files.

Disable Windows Key


This simple demo application demonstrates how to disable the keyboard shortcuts in Microsoft Windows XP to prevent disruption in an application such as a game using C++. Accidentally pressing the Windows key while pressing CTRL or SHIFT keys will suddenly jump the focus off the current application messing up the game experience.

This application shows you how to disable the Windows keys in a windowed app. But the same technique applies for fullscreen as well and the effects of not disabling the keys in fullscreen would be far more disastrous.

How?

In a special windows hook handler function I called "KeyboardProc" I "catch" all WM_KEYDOWN and WM_KEYUP messages. When the keys VK_LWIN or VK_RWIN keys are detected we set a local boolean variable true. VK_RWIN and WKLWIN are the left and right side keyboard Windows Keys. This variable is there to control the return value we send back from KeyboardProc. If we are handling the messages for the Windows key keypresses ourselves (which we are!) then we need to return true or 1 letting Windows know we handled it so don't handle it yourself. If Windows handles VK_RWIN or VK_LWIN it will pop up the Windows menu thing. That's what we're trying to prevent! We only want to prevent the Windows key when our application is in focus or active so we also set a global boolean when we detect that we are active. A windows hook remains active even when an application is minimize and out of focus. You may want to modify this code so that you only disable the keys when in fullscreen. To do that just add another boolean (call "g_bFullscreen" or something like that) and AND it to the criteria that sets bPreventKeypress found in the KeyboardProc function. I went ahead and implemented this but commented it out. I want the Windows Keys disabled even if the app is in windowed mode but active.

Download (Win32 App source code - Visual Studio 2008)

Mouse Leaves Window and App Focus Loss


This demo actually does two things. It detects when the mouse leaves the client area of a win32 window and it also detects when the app has lost focus.

Detecting mouse cursor has left the client area.

There are a few ways to determine when the mouse leaves the window. I chose using the TrackMouseEvent API. On initialization in InitInstance() we try to find the "TrackMouseEvent" handler function and grab its handle. The only problem with the Track Mouse Event API is its only supported on later Windows OSs after Win 98. Win 95 and below is not supported. Because of that if the TrackMouseEvent function isn't found I create my own handler.

The basic way this thing works is while the mouse is moving, this being detected by the WM_MOUSEMOVE handler (in WndProc), we detect if the mouse is already outside the client area of the window. If it is then we call the TrackMouseEvent function and set a flag. Whether its our custom procedure or the one supplied by the API a timer is run. When the time is up we get the current position of the cursor using GetCursorPos() and check if its currently inside our client area. If its not then we or the API will post the WM_MOUSELEAVE message. In that handler (in WndProc) we set our flag variable. GetCursorPos returns screen pixels so it will report correctly even if the cursor is outside our window.

That's sort of in a nutshell what is happening. You can check the code so see for yourself.

Detecting window focus loss

Mouse Focus loss is much simpler and is handled by the WM_ACTIVATE message and WA_INACTIVE parameter. Go look in the WndProc for it.

Download (Win32 App source code - Visual Studio .NET 2003 and 2005)