Multiple keypress issues - Solved

Monkey Targets Forums/HTML5/Multiple keypress issues - Solved

Sensei(Posted 2013) [#1]
Not sure if it's just me doing something stupid, but I've noticed in my game (which I obviously use HTML5 for testing with), that I only seem able to press a single key at a time.
What I mean is, if I wish to move my player down the screen AND press fire at the same, time, the last key pressed stops the previous key from working.

I'm using the Keydown() function. Should I be using KeyHit() instead?


arcsrc(Posted 2013) [#2]
Do you happen to use space as the fire button? If i'm correct most keyboards can't handle downleft and space. It may be something totally different but i've never experienced this issue.


Midimaster(Posted 2013) [#3]
this depends on the keyboard. I made the experience that wireless keyboards have more problems with simultanously pressed keys.

Did you already made a test in BlitzMax?


Sensei(Posted 2013) [#4]
Thanks for the comments guys.

I don't have blitzmax but have blitzbasic. I can knock something up on there to test. I'll also just try to do an xna build tonight to test.
I'm using a USB apple keyboard not wireless.
I do use space as fire. Its common to do so :-)
I'll try set up other key combos for testing too..


computercoder(Posted 2013) [#5]
KeyDown() allows you to detect if the key is still being pressed from frame to frame
KeyHit() checks to see if it was hit in this frame and will not trigger again until its hit again.

Using the following test I made, I am able to get multiple key combinations using HTML5. I am able to get all keys to fire off at the same time and release whenever I release them. I tested this against IE. Couldn't test against the others as this computer only has IE installed.

Import mojo

Class MyApp Extends App

	Method OnCreate()
		SetUpdateRate 15
	End
	
	Method OnUpdate()
	End
	
	Method OnRender()
		Scale DeviceWidth/400.0,DeviceHeight/440.0
		Cls
		DrawText "     Key Code   KeyDown()",0,0
		DrawText "        A         " + KeyDown( KEY_A ),0,20
		DrawText "        D         " + KeyDown( KEY_D ),0,40
		DrawText "        W         " + KeyDown( KEY_W ),0,60
		DrawText "        S         " + KeyDown( KEY_S ),0,80
		DrawText "        SPACE     " + KeyDown( KEY_SPACE ),0,100
		DrawText "        LEFT      " + KeyDown( KEY_LEFT ),0,120
		DrawText "        RIGHT     " + KeyDown( KEY_RIGHT ),0,140
		DrawText "        UP        " + KeyDown( KEY_UP ),0,160
		DrawText "        DOWN      " + KeyDown( KEY_DOWN ),0,180
	End

End

Function Main()

	New MyApp
	
End



Sensei(Posted 2013) [#6]
Cool thanks once again computercoder! I'll test this in a mo. I'm guessing I'm doing something stupid :-)


Edit: So, admittedly it was my stupidity. :)
I was using the same variable for all directions so if you pressed any of the keys it would only ever be one value.
I've changed the code now to use horiztonal, vertical and action variables instead and it works flawlessly now.

I'm such a n00b.
And don't ask to see my ugly code. It might be presentable in a year's time or so, hohoho.


computercoder(Posted 2013) [#7]
Thats cool you got it working :) I'm glad you found the issue.

I *was* going to say something about if you used a single variable, or if you used an elongated If/elseif/endif statement, but I thought I'd leave that be and go for the code test route instead :)


Sensei(Posted 2013) [#8]
Yeah something of a mix really. I've used joyHorizontal for example to be -1, 0 or 1. Same for vertical.
I'll improve on it tonight by using constants instead so LEFT, CENTER, RIGHT.