MaxGUI and EVENT_KEYDOWN not working?

BlitzMax Forums/BlitzMax Beginners Area/MaxGUI and EVENT_KEYDOWN not working?

bubbz(Posted 2006) [#1]
I am trying to detect if the shift key has been held down whilst a GUI button is pressed. In another thread it's stated that EventMods() isn't filled for EVENT_GADGETACTION (my tests suggest this is correct too).

Therefore I chose to set a variable to True when EVENT_KEYDOWN is triggered and EventData() shows shift is pressed, and False for EVENT_KEYUP. Then I can check the state of the variable to know if shift is down whilst a gui button is pressed.

The problem is that 'Case EVENT_KEYDOWN' is never triggered (neither is EVENT_KEYUP). Here's a useless piece of code to demonstrate my point, when launched nothing is printed when keys are pressed:

Strict

Global win:TGadget = CreateWindow("test",10,10,320,240)

Repeat
	WaitEvent()
	
		Select EventID()
		
			Case EVENT_KEYDOWN
			
				Print "keydown"
				
			Case EVENT_KEYUP
				
				Print"Keyup"
				
			Case EVENT_WINDOWCLOSE
			
				End
		
		EndSelect
Forever


I hope somebody can help...


TomToad(Posted 2006) [#2]
You need a Panel or a Canvas to capture KEYUP or KEYDOWN events
Strict

Global win:TGadget = CreateWindow("test",10,10,320,240)
Global pan:TGadget = CreatePanel(0,0,320,240,win,PANEL_ACTIVE)
ActivateGadget pan

Repeat
	WaitEvent()
		Select EventID()
		
			Case EVENT_KEYDOWN
			
				Print "keydown"
				
			Case EVENT_KEYUP
				
				Print"Keyup"
				
			Case EVENT_WINDOWCLOSE
			
				End
		
		EndSelect
Forever



bubbz(Posted 2006) [#3]
Thanks for the reply TomToad.

Now I guess this doesn't help with my detecting if shift is held down when a gui button is pressed since I already have another one of the buttons activated, so that it can be pressed with the return key.

I'm really running out of ideas (I actually can't see why (a) eventmods() cannot be filled for EVENT_GADGETACTION, and (b) why an event cannot be triggered by a keypress regardless of which gadget type is active)

So is there any (simple) way to detect if shift is held down when a gui button is clicked?


Brendane(Posted 2006) [#4]
Windows only?

You can use the win32 GetKeyState function to determine the status of the shift keys at the moment you process the button click :-

Strict 

' Import win32 function GetKeyState()
' See MSDN for documentation on this function : basically the key is down if the high order bit 
' is set (and the return value is a short so this bit is $8000
Import pub.win32
Extern "win32"
	Function GetKeyState:Short(vkey)="GetKeyState@4"
End Extern 

' Define the constants we're interested in (you can look these up on MSDN)
Const VK_LSHIFT = $000000A0
Const VK_RSHIFT = $000000A1


Local win:TGadget = CreateWindow ("Test",10,10,200,200)
Local button:TGadget = CreateButton("Clickme with/without L/R shift",10,10,170,24,win,BUTTON_PUSH)

While WaitEvent()
	Select EventID()
		Case EVENT_WINDOWCLOSE
			End
			
		Case EVENT_GADGETACTION

			If GetKeyState( VK_LSHIFT ) & $8000  Or GetKeyState( VK_RSHIFT ) & $8000
				Notify( "Shift is down" )
			Else
				Notify( "Shift not down" )
			EndIf
	EndSelect 
Wend


Hope that solves your problem!


TomToad(Posted 2006) [#5]
If you use the BUTTON_OK style, you should be able to use the Enter key without having to activate the button.