Disable Windows Key

BlitzMax Forums/BlitzMax Programming/Disable Windows Key

Does any one have any code to disable the windows key. To Prevent minimize.

Why not...
If AppSuspended()
'wait til Not AppSuspended()
EndIf


You could use windows hooks, heres an example using a low-level keyboard hook that disables the left WIN key and the right WIN key as long as the hook is active.

see Windows Hooks for more info.

hook.c
#include <windows.h>

#define EXPORT __declspec(dllexport)

static HHOOK current_hook;

EXPORT DWORD keyboard_hook( int ncode, WORD wparam, KBDLLHOOKSTRUCT *lparam) {
	if( ncode < 0 || ncode != HC_ACTION) return CallNextHookEx( current_hook, ncode, wparam, (DWORD)lparam);
	if( wparam == WM_KEYDOWN || wparam == WM_SYSKEYDOWN || wparam == WM_KEYUP || wparam == WM_SYSKEYUP) {
		switch( lparam->vkCode) {
			// keys to disable
			case VK_LWIN:
			case VK_RWIN:
				CallNextHookEx( current_hook, ncode, wparam, (DWORD)lparam);
				return 1;
		}
	}
	return CallNextHookEx( current_hook, ncode, wparam, (DWORD)lparam);
}

EXPORT int init_hook( HHOOK hook) {
	if(hook) {
		current_hook = hook;
		return TRUE;
	}
	return FALSE;
}

Compile dll:
gcc -shared hook.c -o hook.dll

Hook.bmx
SuperStrict

Const WH_MIN:Int = -1
Const WH_MSGFILTER:Int = -1
Const WH_JOURNALRECORD:Int = 0
Const WH_JOURNALPLAYBACK:Int = 1
Const WH_KEYBOARD:Int = 2
Const WH_GETMESSAGE:Int = 3
Const WH_CALLWNDPROC:Int = 4
Const WH_CBT:Int = 5
Const WH_SYSMSGFILTER:Int = 6
Const WH_MOUSE:Int = 7
Const WH_HARDWARE:Int = 8
Const WH_DEBUG:Int = 9
Const WH_SHELL:Int = 10
Const WH_FOREGROUNDIDLE:Int = 11
Const WH_CALLWNDPROCRET:Int = 12
Const WH_KEYBOARD_LL:Int = 13
Const WH_MOUSE_LL:Int = 14
Const WH_MAX:Int = 14
Const WH_MINHOOK:Int = WH_MIN
Const WH_MAXHOOK:Int = WH_MAX

Const INIT_HOOK_NAME:String = "init_hook"

Global IsHookLoaded:Int
Global IsHooked:Int

Private
	Global Handle:Int
	Global InitHookProc:Int( hook:Int)
	Global HookProc:Byte Ptr
	Global CurrentHook:Int
	
	Extern "Win32"
		Function LoadLibrary:Int( name$z) = "LoadLibraryA@4"
		Function GetProcAddress:Byte Ptr( handle:Int, name$z)
		Function FreeLibrary:Int( handle:Int)
		Function SetWindowsHookEx:Int( hooktype:Int, hookproc:Byte Ptr, handle:Int, param:Int) = "SetWindowsHookExA@16"
		Function UnhookWindowsHookEx:Int( hook:Int)
	EndExtern
	
	Function on_end_hook()
		FreeHook()
	EndFunction
	
	OnEnd on_end_hook
Public

Function LoadHook:Int( hookdll:String)
	FreeHook()
	Handle = LoadLibrary( hookdll)
	If Handle = 0 Then Return False
	InitHookProc = GetProcAddress( Handle, INIT_HOOK_NAME)		
	If Not InitHookProc Then
		FreeLibrary( Handle)
		Return False
	EndIf
	IsHookLoaded = True
	Return True
EndFunction

Function InitHook:Int( hooktype:Int, hookname:String)
	If Not IsHookLoaded Then Return False
	ReleaseHook()
	HookProc = GetProcAddress( Handle, hookname)
	If HookProc Then
		CurrentHook = SetWindowsHookEx( hooktype, HookProc, Handle, 0)
		If CurrentHook <> 0 Then
			IsHooked = InitHookproc( CurrentHook)
			Return IsHooked
		EndIf
	EndIf
	Return False
EndFunction

Function ReleaseHook:Int()
	If Not IsHookLoaded Or Not IsHooked Then Return False
	UnhookWindowsHookEx( CurrentHook)
	IsHooked = False
	Return True
EndFunction

Function FreeHook:Int()
	If Not IsHookLoaded Then Return False
	ReleaseHook()
	FreeLibrary( Handle)
	IsHookLoaded = False
	Return True
EndFunction

test.bmx
SuperStrict

Import "Hook.bmx"

If LoadHook( "hook.dll") Then
	Print "Hook is loaded."
	If InitHook( WH_KEYBOARD_LL, "keyboard_hook") Then
		Notify "Hook is active, waiting for termination"		
	Else
		Print "error:InitHook() failed"
	EndIf
Else
	Print "error:LoadHook() failed"
EndIf
FreeHook()


I don't know what any of that does. Will the user still be able to right-click on the task bar and select 'show desktop'? Because that minimises everything thats open.

Its far better to get your program to recognise and operate safely in a minimised state, than trying to stop the user minimising it in the first place.

You should do both, its not very fun to drop out of the game just because you hit the wrong key.

Other things that could be disabled are the Accessibility keys. (they are allso annoying ;)

EDIT: yeah the ShowDesktop menu will probably still work.

What GfK says. People who mess up Windows deserve to watch Tomb Raider in hell for all eternity.

Other things that could be disabled are the Accessibility keys.
You're supposed to disable those yourself, if you don't need them.

Thanx for all the replies/code guys, I will try these.
I don't want to completely do away with minimizing. It's just an option for certain things!

I can get my apps to minimize and maximize alright - The only problem i seem to have is while minimized the music still plays! So i need to trap the input and pause/disable the sound channel during that time.

The only problem i seem to have is while minimized the music still plays! So i need to trap the input and pause/disable the sound channel during that time.
AppSuspended() is what you need.

Thanx Gfk...

I will try this...

Well thats worked well so far, I am now patching and updating my latest 3 Bmax game releases!

I may post them in the Showcase later for testing and feedback :)

Thanx again everyone!