How to handle a keypress

BlitzPlus Forums/BlitzPlus Programming/How to handle a keypress

WolRon(Posted 2004) [#1]
I have a message handler like this:
Repeat
	EvntID = WaitEvent()
	Select EvntID
		Case $102
			If EventData() = 1
				result = Confirm("Terminate Game?")
				If result = True Then End
			EndIf
	End Select
Forever
and it pops up the Confirm box when I press the escape key. The problem is if I click on a button, and then press the escape key, the Confirm box does not pup up.

What am I doing wrong? How do I make it so that at any time the user can press the escape key and the Confirm box will pop up?


Cold Harbour(Posted 2004) [#2]
I think you need to transfer focus from the button back to the main window after it's pressed. Something like

Case $401

    button stuff
    ActivateGadget mainwindow



WolRon(Posted 2004) [#3]
Thank you. That worked. I wasn't sure how to set the focus back to the main window.


However, I've now realized a small flaw.

If I click on the button (mousedown, mouseup), it works fine.

If I click on the button (mousedown), but then drag the mouse off of the button and only then release the mouse (mouseup), the focus stays on the button.

Do you know how I may handle that situation?


pantsonhead.com(Posted 2004) [#4]
You could set up a timer to switch focus back to the mainWindow every half second or so (assuming the focus should always be on the mainWindow).


WolRon(Posted 2004) [#5]
That's not a perfect solution either unfortunately.


WolRon(Posted 2004) [#6]
By setting up a timer, it does set the focus back to the main window, BUT

If you press a button down, and while you are pressing it down, the timer happens to go off, the button will reset (go back up) even though you didn't release the mouse button yet.

This is definitely not something I want to happen.

Does anyone have any ideas on how to handle catching a key being pressed (without the possibility of it not being detected)?


WolRon(Posted 2004) [#7]
I figured out how to make it work. I needed to use a HotkeyEvent and reactivate my main window at the end of the message handler

HotKeyEvent(1, 0, $102, 0, 0, 0, 0, WinMain)

;message handler
Repeat
	EvntID = WaitEvent()
	Select EvntID
		Case $102
			If EventData() = 1
				result = Confirm("Terminate Game?")
				If result = True Then End
			EndIf
	End Select
	ActivateGadget WinMain
Forever