Weird KeyDown behaviour

Archives Forums/BlitzMax Bug Reports/Weird KeyDown behaviour

simonh(Posted 2013) [#1]
Don't want to post this as a bug report yet as it's probably something I'm doing wrong - it's been a while since I've used BlitzMax! Moved to bug forum

Basically KeyDown seems to be unreliable if the mouse pointer is moving. Here's an example program:

Graphics 640,480

While Not KeyHit(KEY_ESCAPE)

	Local shift_down=KeyDown(KEY_LSHIFT)

	i=i+1
	If shift_down
		Print i + ". shift_down: " + shift_down
	EndIf
	
	Flip

Wend


When pressing left shift and the mouse pointer is still, the program above works as expected (something is printed while shift is held down).

If you start waving the mouse pointer about while holding down shift, then you'll get unreliable results (sometimes something is printed while shift is not being held down, or sometimes nothing is printed while shift is held down).

I'm using OS X Lion and the latest version of BlitzMax. Anyone have any idea what the problem might be?


simonh(Posted 2013) [#2]
Further investigation (don't you just love it when you waste hours on something so trivial?) reveals that this only happens with the 'modifier' keys (shift, ctrl, alt).


GfK(Posted 2013) [#3]
Noticed the same thing with my game editor - sometimes have to press and release CTRL to get it to realise that the key isn't being pressed any more.

Didn't report it as I thought it might be something to do with my crap keyboard.


simonh(Posted 2013) [#4]
Managed to sort out a fix for this.

In brl.mod/system.mod/system.macos.m, in bbSystemEmitOSEvent, comment out this line like so:

//mods = [event modifierFlags];

Now you need to re-add this line to the two case statements that need it. So that's NSKeyDown, and NSFlagsChanged. Add it to the top of each case block, like so:

case NSKeyDown:
	mods=[event modifierFlags];

case NSFlagsChanged:
	mods=[event modifierFlags];

The problem was due to the mods value being changed by any event, including moving the mouse, interfering with the modifier keys code.

Surprised no-one has found this bug before now - damn annoying for things like editors!


GaryV(Posted 2013) [#5]
Good find. Hopefully this will make it into a future update.


simonh(Posted 2013) [#6]
There seems to be a further bug that means that when the command left/right keys are held down, then the NSKeyUp event doesn't get sent for any other keys - meaning that KeyDown can get 'stuck'. Not sure how to fix this one, as it seems the system is not firing the NSKeyUp event with the command keys held down.

So for editors on the Mac, probably best to use ctrl instead of command for things like copy and paste.