KeyDown returned as Int

Monkey Forums/Monkey Programming/KeyDown returned as Int

Raz(Posted 2012) [#1]
Hi,

is there any particular reason why KeyDown returns an Int instead of a Bool? I'm surprised it's the first time it has come up, but I've just had the following error when trying to store the state of KeyDown in a Bool variable.

Error : Cannot convert from Int to Bool.



Goodlookinguy(Posted 2012) [#2]
That is strange, but not unexpected. As the mojo code has no return set, it'll return an int in non-strict mode.

Function KeyDown( key )
	Return device.KeyDown( key )
End


Most people are going to use KeyDown like this, so it usually isn't a problem.

If KeyDown(KEY_X)
	' Do something
End


I wouldn't say you're off though, that this isn't right. It probably should return boolean instead of an integer.


Foppy(Posted 2012) [#3]
This was also mentioned about TouchDown() here:

http://www.monkeycoder.co.nz/Community/posts.php?topic=994

Mark agreed that the "down" functions should probably return a Bool instead of an Int. (Returning Int makes more sense for the "hit" functions.)


Raz(Posted 2012) [#4]
Most people are going to use KeyDown like this, so it usually isn't a problem.


Yeah, that's probably why I've not seen it before. I can't think of the proper name for it, but I'm making a middle man kind of control system so that bool variables for moving/aiming can be set by the keyboard, touch controls or the joypad.


taumel(Posted 2012) [#5]
The way monkey deals with input could generally be improved.

For instance MouseDown() doesn't only get you a positive response during the frame it got called for the first time like in many other languages where they differ between Mouse, MouseDown, MouseHit. Instead it's true every frame which forces you to keep track of these things on your own. It's more work and confusing when you're jumping between different languages.


slenkar(Posted 2012) [#6]
mousedown and mousehit have always worked like that and no-one else complains about them


Gerry Quinn(Posted 2012) [#7]
There may be other functions with similar names in some languages. But you really need an async function that tells you when the mouse is down now.

Use MouseHit() to tell if it's been clicked since the last update.


taumel(Posted 2012) [#8]
@slenkar
Actually that's a great argumentation.

@Gerry Quinn
Let's put it this way, monkey's implementation needs more work to be done, depending on what you're trying to achieve, and it isn't common whilst also offering no benefit on top.


muddy_shoes(Posted 2012) [#9]
it isn't common whilst also offering no benefit on top


Well, there is a benefit to BRL in not having to write/support a more complete input library. You could argue that it's impossible to predict how people would want to abstract across the mouse/touch interfaces so it's best not to try.

For my own stuff I wrap the mojo input module in a pseudo-event system that provides KEY_DOWN/KEY_UP events as well as other nice-to-haves like MOUSE_DRAG info.


taumel(Posted 2012) [#10]
In my opinion Input is one of the core features which i would try to get done as good as possible. Btw. i remember that the first problem a friend of mine, i once suggested monkey to, had was that his code was flipping through his menus due to the unexpected behaviour of MouseDown.


Gerry Quinn(Posted 2012) [#11]
I don't find the Monkey system unusual. Mojo isn't really event driven except at the most basic level, so why would I expect MouseDown() to tell me anything other than whether the mouse is down?

Generally the function called during every update starts:

If MouseHit( MOUSE_LEFT ) > 0



Raz(Posted 2012) [#12]
For what it's worth, I'm happy with the way the mouse/key functions work, except the Down functions should really return a bool.