KeyDown() without Graphics()

BlitzMax Forums/BlitzMax Programming/KeyDown() without Graphics()

JoshK(Posted 2006) [#1]
I need to set up a Keydown() function to work either with a window, or without anything at all. I am not using the Blitz Graphics() command.


FlameDuck(Posted 2006) [#2]
You need to use events instead.


JoshK(Posted 2006) [#3]
Those would be fine as well, but they don't work without a canvas.


REDi(Posted 2006) [#4]
You can use a panel with PANEL_ACTIVE set, then you'll get events.


JoshK(Posted 2006) [#5]
Sweet!


CS_TBL(Posted 2006) [#6]
pah, still sux.

1: You need to click on the panel before it creates key-events
2: still no key-repeat (event id 515), so cursor-repeat, as well as other keys (F-keys etc.). So no real use for keyboard-based editing yet.

In B+ you didn't need any panel or canvas.. keys were just there..


JoshK(Posted 2006) [#7]
Use ActivateGadget() to activate the panel.


CS_TBL(Posted 2006) [#8]
It still doesnt' fix issue 2.

I'd say it's a feature-lack for the moment.. I rather have no requiring panels or canvases for keypresses, just like B+..


REDi(Posted 2006) [#9]
The key is pressed until you get an EVENT_KEYUP


klepto2(Posted 2006) [#10]
IF you use WinXp and have MinGW properly installed you could do a hack to the Win32Gui Module.

For details : Take a look at http://www.blitzbasic.com/Community/posts.php?topic=55069#614872

If change it to : setEventMask( BBEVENT_MOUSEMASK|BBEVENT_KEYMASK,this ) then

you have to call EnablePolledInput before your main loop and the KeyDown and KeyHit also MouseX and Y and MouseHit commands will work.

PS: Works for nearly every Gadget but not for the DesktopGadget


JoshK(Posted 2006) [#11]
Try this:

Global KeyDown[256]

Function UpdateKeyboard()
	Select EventID()
		Case EVENT_KEYDOWN
			KeyDown[EventData()]=True
		Case EVENT_KEYUP
			KeyDown[EventData()]=False
	EndSelect
End Function



klepto2(Posted 2006) [#12]
If you want to use a panel , just make something like this before your mainloop:


EnablePolledInput()

While WaitEvent()<>EVENT_WINDOWCLOSE
'Here you could now use the KeyDown and KeyHit commands as usual

Wend




CS_TBL(Posted 2006) [#13]
btw, I'm not looking for keyrepeat as used in games (no delay).

But more like:

[press][delay][repeat][repeat][repeat][repeat][repeat][repeat][repeat][repeat][repeat][keyup]

The delay is important.

see this B+ code:

window=CreateWindow("._.",0,0,640,480)

canvas=CreateCanvas(0,0,640,480,window)

Repeat
	WaitEvent()
	If EventID()=$803 End
	
	SetBuffer CanvasBuffer(canvas)
		Cls
		Text 2,2,EventID()+" "+EventData()
		
		If EventID()=259 ; keyrepeat
			If EventData()=63232 ; up
				y=y-8
			EndIf
			If EventData()=63233 ; dn
				y=y+8
			EndIf
			If EventData()=63276 ; pgup
				y=y-32
			EndIf
			If EventData()=63277 ; pgdn
				y=y+32
			EndIf
			If EventData()=63273 ; home
				y=0
			EndIf
			If EventData()=63275 ; end
				y=400
			EndIf
			If EventData()=63234 ; l
				x=x-8
			EndIf
			If EventData()=63235 ; r
				x=x+8
			EndIf
			If EventData()=63236 ; F1
				x=Rnd(600)
				y=Rnd(400)
			EndIf
		EndIf
		Rect x,y,8,8,1
	FlipCanvas canvas
Forever


Things should work/feel exactly like this.. I dunno about all that polled stuff, I basically want to do events-only orso.. all my keyboard routines ar meant for apps, not games.
At the moment it isn't possible with bmax-events natively, unless someone proves me wrong. :P
(oh, and I don't have xp/mingw)

ps. note that I only used a canvas here to visualise the effect, a canvas or panel is not required.


klepto2(Posted 2006) [#14]
If you want to handle it this way, you have to hack the Win32Gui Source as mentioned above and then catch the events like in B+.


CS_TBL(Posted 2006) [#15]
hm.. too complex for me..

I ready-made solution (meaning: EVENT_KEYCHAR is created by a window) is appreciated tho. ^_^


JoshK(Posted 2006) [#16]
Dude, just use EnablePolledInput().

For Keydown() events, I usually just enter a loop and don't exit until the KeyUp() event is encountered.


CS_TBL(Posted 2006) [#17]
Then convert that B+ source put there above to bmax please, as small as possible and with the natural delay. :P


Defoc8(Posted 2006) [#18]
lol@... liberal use of the word, nevermind ;)


degac(Posted 2006) [#19]
!!!!


Dude, just use EnablePolledInput().

For Keydown() events, I usually just enter a loop and don't exit until the KeyUp() event is encountered.




Please give an example! I'm going crazy...
I've tried something like this
enablepolledinput()
Repeat
Print "..."
If EventID()=EVENT_KEYUP Print "key pressed";go_exit=1
Until go_exit=1
End


I used PollEvent() but nothing to do..
but nothing to do: and I dont' want a WaitEvent() call.


CS_TBL(Posted 2006) [#20]
Why don't you want a WaitEvent?? What's more glorious and luxury than an event system??


degac(Posted 2006) [#21]
because WAITEVENT() halts program execution until an event is available: this mean that other processes are stopped...


CS_TBL(Posted 2006) [#22]
What other processes? game-updates/AI? Stick them to an EVENT_TIMERTICK :P You *can* create games with events you know..


JoshK(Posted 2006) [#23]
enablepolledinput()
Repeat
Print "..."
If KeyDown(KEY_UP) end
forever


degac(Posted 2006) [#24]
Strict

Local win_main:tgadget=CreateWindow("Test",50,50,300,200,Null,WINDOW_TITLEBAR)

enablepolledinput()
While True
	WaitEvent 
	Print CurrentEvent.ToString()
	Select EventID()
		Case EVENT_KEYDOWN
			Print "key pressed"			
		Case EVENT_WINDOWCLOSE
			End
	End Select
Wend

This program emits or not a KeyDown Event? On my computer I CAN NOT catch a keyboard event...
:(
Please gimme an hint, I lost a week of life for this...

@Halo: thanks for the example but I didn't work...


Diablo(Posted 2006) [#25]
i think it only lets you use EVENT_KEYDOWN when a canvas is active.

enabledpolledinput() just lets you use keydown() keyhit() and such like.

Strict

Local win_main:tgadget=CreateWindow("Test",50,50,300,200,Null,WINDOW_TITLEBAR)
Local can_main:tgadget=CreateCanvas(0,0,ClientWidth(win_main),ClientHeight(win_main), win_main)

ActivateGadget can_main

enablepolledinput()
While True
	WaitEvent 
	Print CurrentEvent.ToString()
	Select EventID()		
		Case EVENT_WINDOWCLOSE
			End
	End Select
	
	If KeyHit(KEY_ESCAPE) Then End

Wend




degac(Posted 2006) [#26]
So only with a canvas active I can intercept key event...
Maybe I've misinterpreted the command 'enablepolledinput()'
Thanks