Linux: AppSuspend()-bug (ALT-Key-State + Alt-Tab)

BlitzMax Forums/BlitzMax Programming/Linux: AppSuspend()-bug (ALT-Key-State + Alt-Tab)

Derron(Posted 2016) [#1]
If you run the following code:



What does happen?

For me (Linux) the state of the "ALT"-Key is not reset after switching apps via "ALT+TAB".

If that happens to others too: what ways do we have to fix this? I assume, after the app gets out of focus (tabbed to background) it should reset key-state.


bye
Ron


grable(Posted 2016) [#2]
hows about using AppSuspended() ?

SuperStrict

Framework Brl.StandardIO
Import Brl.GLMax2D

Graphics 400,300

Global WasSuspended:Int = False

SetScale 2.0, 2.0
Repeat
	Cls

	If AppSuspended() Then
		If Not WasSuspended Then
			' enter suspended state
			WasSuspended = True
			FlushKeys()
		EndIf
	ElseIf WasSuspended Then
		' leave suspended
		WasSuspended = False
	EndIf
	
	
	If KeyDown(KEY_LALT)
		DrawText("Left ALT-key is down", 40,50)
		DrawText("(time: "+MilliSecs()+")", 40,80)
	Else
		DrawText("Left ALT-key is up", 50,50)
		DrawText("Now try Alt-Tabbing...", 30,80)
	EndIf
	Flip
Until KeyHit(KEY_ESCAPE) Or AppTerminate()



seriouslee(Posted 2016) [#3]
Do do you mean the app shows the left alt key down when it's not, or it shows the left alt key up when it's down?

I'm using NG with Windows 10 and running the application. When I start to cycle through the message states the left alt is up and stays that way until the app grabs focus. If I have the alt key down through the whole process I must release it and press it again for the program to respond.

It's not just the Alt key. If I add the left shift key to the mix, it will do the same thing. In looking at brl.polledinput source (I'm guessing that's what is being used) it looks to me like it's keeping track of Event state with an event hook function. So, it won't register a KeyDown event (and update it's keystate variable) until the OS sends one, which it won't if the key is already down.

Does that logic sound right to you?


Derron(Posted 2016) [#4]
I am not able to try now...

always assumed "supended" means "sent to sleep"


@seriouslee
i start the program.
Next i test left-alt... down and up (messages change?)
Then I alt-tab out ... and alt-tab in
Message in the app states, that alt-key is down.
Pressing left-alt again...corrects the state

@grable
Will check the code then..assume you tried it so it surely will work.

Maybe we should patch polledinput.mod (in github.com/maxmods)

Bye
Ron


Derron(Posted 2016) [#5]
@ Grable,

Tried you code - did not help.

adjusted it to:
	If AppSuspended() Then
		If Not WasSuspended Then
			print "suspending"
			' enter suspended state
			WasSuspended = True
			FlushKeys()
		EndIf
	ElseIf WasSuspended Then
		print "unsuspending"
		' leave suspended
		WasSuspended = False
	EndIf

And "suspending" is never printed.


Looking at brl.PolledInput:
- it hooks into some events
- events are "suspend" or "keyDown"/"keyUp" etc.

This means: if "AppSuspended()" would work here, then "KeyUp" would be received too (and then the keystate would be correct).


Edit:
brl.mod/system.mod/system.linux.c does not contain handling of "AppSuspend" while system.win32.c does ...
So this AppSuspend() might be something only working on Windows-platforms.


bye
Ron


col(Posted 2016) [#6]
Derron,

Just to let you know and maybe help isolate the issue... using grables code with your adjustments on Windows 10 I get 'suspending' and 'unsuspending' shown in the output.


Derron(Posted 2016) [#7]
Like said, the issue is, that for Linux Mark did not implement AppSuspended() and AppResume().

On Client-messages only "AppTerminate" is processed. I played around with the X11-event "WM_TAKE_FOCUS" (should happen if the app starts or gets focues) but I wasn't able to make it do something ... also I did not find the opposite event (loosing focus).

The reason might be, that in an "X11-world" there is no real "loose focus" as a executable might run on another computer screen etc.
I do not need "focus gained" and "focus lost", "focus lost" would be enough (to flush keys).



So for now it seems that handling "left alt"-key in your linux-blitzmax-app is not possible that easily.


Edit: Adjusted thread title to reflect real issue.

bye
Ron


Derron(Posted 2016) [#8]
Ok ... So I assume to have found the solution:

the graphical-Window is not created with the "FocusChangeMask". Adding that means sending out FocusIn/FocusOut events to potential listeners.

Now adding these to system.mod - and "bam", AppSuspend and appResume events are coming in on "alt-tab".

Ok, dunno if that is desired, but: when moving the window (dragging the title bar) it receives the "suspended" event too...
On windows the app stays "active" while being dragged around (albeit I remember to have issues with this - that the app does not run when being moved around, was that way on XP).


Will create a pull request for maxmods and edit in the link here.
Edit: here is the link
https://github.com/maxmods/brl.mod/pull/9/commits/f939a9d1987073ad35bbe6b8de9bf44e1abba5e7

bye
Ron