polled input on a MaxGUI canvas?

BlitzMax Forums/BlitzMax Beginners Area/polled input on a MaxGUI canvas?

Idiot(Posted 2005) [#1]
I'm trying to do something similar to polled input, but the key down and key up events don't seem to tell me if a key is being held down, only that it was hit, and KeyDown() only appears to work in a Graphics mode.

In graphics mode, this is just as simple as:

if keydown(KEY_LEFT) then x=x-1

But how do you do it with events and all that?


Arne(Posted 2005) [#2]
Edit: Maybe this wasn't what you were asking, but someone else might have use of it, or a comment on my crummy code.

I have this old wonky function I wrote ages ago. I'm sure there's some better solution. I haven't looked at polled input yet.

Graphics 640,480,0 ' Windowed

Global useleft, useright, useup, usedown ' Keys
Global x=320,y=240 ' Player

SetClsColor 16,31,63
SetColor 255,191,63


While Not KeyHit(KEY_ESCAPE)

	Cls
	check_input()
	check_player()
	
	DrawRect x-8,y-8,16,16
	Flip

Wend



Function check_player()

	If useleft=1
	useleft=0
	x=x-2
	EndIf
	
	If useright=1
	useright=0
	x=x+2
	EndIf
	
	If useup=1
	useup=2
	y=y-8
	EndIf
	
	If usedown=1
	usedown=2
	y=y+8
	EndIf
	
End Function 


Function check_input()

	If useleft=0 And KeyDown(KEY_LEFT) Then useleft=1
	If Not KeyDown(KEY_LEFT) Then useleft=0
	
	If useright=0 And KeyDown(KEY_RIGHT) Then useright=1
	If Not KeyDown(KEY_RIGHT) Then useright=0
	
	If useup=0 And KeyDown(KEY_UP) Then useup=1
	If Not KeyDown(KEY_UP) Then useup=0
	
	If usedown=0 And KeyDown(KEY_DOWN) Then usedown=1
	If Not KeyDown(KEY_DOWN) Then usedown=0

End Function



Perturbatio(Posted 2005) [#3]
I don't think that's what he was looking for, nice try though :)

I don't think there is an event that currently allows this, the EVENT_KEYDOWN, EVENT_KEYUP, EVENT_KEYCHAR and EVENT_HOTKEYHIT are the only key events in MaxGUI.


Perturbatio(Posted 2005) [#4]
you *could* do something like this:



Idiot(Posted 2005) [#5]
Let me restate this as it's probably overly simple and I'm missing something:

I want to move a character if certain keys are being held down. I am not using a graphics mode, I am using a canvas. How do you do this? Keydown() doesn't seem to work.


Perturbatio(Posted 2005) [#6]
Using the same code I posted above:



Chroma(Posted 2005) [#7]
Hmm...now this isn't working for me either...


Chroma(Posted 2005) [#8]
Big FYI...the keycodes aren't the same so you have to make your own keycode list for this to work. ie the escape key is 27.


Yan(Posted 2005) [#9]
I'm confused, 27 is what my docs say KEY_ESCAPE should be?


Perturbatio(Posted 2005) [#10]
yup, that's what it should be.


wedoe(Posted 2006) [#11]
I think this is simpler:


blah blah

Global key$

While True
 Select EventID()

  Case EVENT_KEYDOWN
   key$=EventData()

  Case EVENT_KEYUP
   key$=""

  Case EVENT_GADGETPAINT

   blah blah

   If key$="37" then x=x-1 ' Key Left
   If key$="39" then x=x+1 ' Key Right

End Select

Wend



ImaginaryHuman(Posted 2006) [#12]
I don't know if this was said already, too lazy to read it all, but basically the key events are the kind of low-level events that that o/s itself would generate and handle.

First you assume that you start out with all the keys unpressed, and FlushKeys is automatically called.

When a user presses a key, upon the initial contact of the key with its sensor it sends a signal to say "this key was just pressed", and sends the keydown event for that key. It's not a `key is being held` event, it's a `the key WAS pressed at some point` event. YOU have to be the one that is responsible for using this information to interpret it to mean that the key is being `held down`. Ie, once you receive a key down event, you should set your own boolean flag to say that it is being held, and then wait until there's a key up event - upon which you then clear the flag to say the key is no longer held.

You have to manage the keypresses yourself, basically. You have to implement your own code to keep track of the order of key events and translate it into `key is held down` and `key is released` events. Basically if you received a key down event but you diidn't receive a key up event for the same key yet, you can assume it's being held down.


Helios(Posted 2006) [#13]
EnablePolledInput()? Probably the easiest way.

Crude example:
SuperStrict

Framework brl.glmax2d
Import brl.polledinput

?win32
Import brl.win32maxgui	
?macos
Import brl.cocoamaxgui
?linux
Import brl.fltkmaxgui
?

Local hWin:TGadget = CreateWindow("Polled Input Test",20,20,640,480,Null,WINDOW_TITLEBAR)
Local hCanvas:TGadget = CreateCanvas(0,0,ClientWidth(hWin),ClientHeight(hWin),hWin)

SetGraphics CanvasGraphics(hCanvas)
EnablePolledInput()
ActivateGadget(hCanvas)

Repeat
	If KeyDown(KEY_LEFT) Then DrawText("Left",15,15)
	If KeyDown(KEY_RIGHT) Then DrawText("Right",15,30)
	If KeyDown(KEY_UP) Then DrawText("Up",15,45)
	If KeyDown(KEY_DOWN) Then DrawText("Down",15,60)	
	If MouseDown(1) Then DrawText("Mouse left",15,75)
	If MouseDown(2) Then DrawText("Mouse right",15,90)
	DrawText("Press arrow keys or click mouse to test polled input!",5,GraphicsHeight()-20)
	Flip;Cls
Forever