Any way to ignore Alt key when using menu gadgets

BlitzPlus Forums/BlitzPlus Programming/Any way to ignore Alt key when using menu gadgets

NetGamer(Posted 2005) [#1]
I am using various combinations of Alt, Ctrl, and Shift with mouse clicks and mouse wheel actions as the input for my program. This program runs on the desktop and has a typical menu bar which is initialized as follows:

function InitMenu()
; -----------------------------------------------------------------------------
; Create a menu...
; -----------------------------------------------------------------------------

; Note that the & sign in these examples underlines the next letter, and a
; blank menu name creates a 'bar'...
.InitMenu
mainmenu = CreateMenu ("&Game", MENU_GAME, WindowMenu (window))
mMenuGameConfig = CreateMenu( "&Config", MENU_GAME_CONFIG, mainmenu)
mMenuGameSave = CreateMenu( "&Save",   MENU_GAME_SAVE, mainmenu)
mMenuGameLoad = CreateMenu( "&Load",   MENU_GAME_LOAD, mainmenu)
mMenuGameOffline = CreateMenu( "&Offline",MENU_GAME_OFFLINE, mainmenu)
mMenuGameHost = CreateMenu( "&Host",   MENU_GAME_HOST, mainmenu)
mMenuGameJoin = CreateMenu( "&Join",   MENU_GAME_JOIN, mainmenu)
mMenuGameExit = CreateMenu( "E&xit",   MENU_GAME_EXIT, mainmenu)

mMenuGameHelp = CreateMenu( "&Help",   MENU_HELP, WindowMenu(window))
mMenuGameControls = CreateMenu( "&Controls",   MENU_HELP_CONTROLS, mMenuGameHelp)
CreateMenu( "", MENU_HELP_BLANK, mMenuGameHelp)
mMenuGameAbout = CreateMenu( "&About", MENU_HELP_ABOUT, mMenuGameHelp)


enablemenu mMenuGameHost
enablemenu mMenuGameJoin
enablemenu mMenuGameConfig
disablemenu mMenuGameOffline
uncheckmenu mMenuGameHost
uncheckmenu mMenuGameJoin
enablemenu mMenuGameExit

UpdateWindowMenu window ; This MUST be called after creating your menu!
end function
;###############################################################################
function EventKeyDown()
				Select EventData()
					Case SC_CTRL
						KBCtrlState = True
					Case SC_SHIFT
						KBShiftState = True
					Case SC_ALT
						KBAltState = True
				End Select
end function
;###############################################################################
function EventKeyUp()
				Select EventData()
					Case SC_CTRL
						KBCtrlState = False
					Case SC_SHIFT
						KBShiftState = False
					Case SC_ALT
						KBAltState = False
				End Select
end function


I track the state of the Alt, Ctrl & Shift keys via a case statement in the main loop based on the key up and key down events.

How do I disable the Alt key from interacting with the menu so when someone does an Alt-Left-Mouse-Button it doesn't activate the first menu entry?


Grey Alien(Posted 2005) [#2]
I stick this in my main loop:

If KeyHit (LEFT_ALT) Then			
	User32_keybd_event(255,0,0,0)
	User32_keybd_event(255,0,KEYEVENTF_KEYUP,0)
EndIf

this just prevents left alt from making the main menu appearing and the game pausing, it may not be what you want. To get this to work you need to add this to user32.decls
User32_keybd_event%(bVk%, bScan%, dwFlags%, dwExtraInfo%) : "keybd_event" 

and these are the consts you need
Const LEFT_ALT = 56
Const KEYEVENTF_KEYUP = 2 ;used with Keybd_event



NetGamer(Posted 2005) [#3]
That looks like just what I wanted - to ditch the message before it gets to the menu gadget but still be able to set my internal state variable.

How about the right Alt key?


NetGamer(Posted 2005) [#4]
This works great. Here's the code snippet I used:

;   Prevent ALT key from bringing up the menu when released
	If KeyHit(SC_RIGHT_ALT) Or KeyHit(SC_LEFT_ALT) Then
		api_keybd_event(255,0,0,0)
		api_keybd_event(255,0,KEYEVENTF_KEYUP,0)
	End If

I use the USER32 decl file posted here so the syntax was adjusted to match (USER32 -> api).

The really nice part about this is that the menu still reveals the shortcut keys (underlines the single character in the menu entry) while the Alt key is depressed and correctly handles a shortcut (Such as Alt-F for file).

Now, can anyone explain what these two USER32 calls are actually doing (and what each parameter means)?


Grey Alien(Posted 2005) [#5]
Glad it worked. The api calls are making a fake key down and key up of key 255, this seems to fix the problem because the system think you are holding key 255 (a non existant key) with Alt and thus the menu doesn't appear as it only appears withpure alt. tada.