Event Handler with some keys

Discuss any general programming issues here
User avatar
Tarzin
Posts: 112
Joined: Mon Feb 15, 2010 11:46 am
Location: Dunkerque / FRANCE
Contact:

Event Handler with some keys

Post by Tarzin »

Does it work??

Code: Select all

        Function p_HandlerFunc(msg)

                Switch(msg.action)

                Case "OnKeyDown":
                        If msg.key = "RETURN" Then End
                EndSwitch

        EndFunction

        InstallEventHandler({OnKeyDown = p_HandlerFunc})

        Repeat
                WaitEvent
        Forever
Nothing appears at home with Windows IDE 4.7 and Windows Vista

I've tried with success UP, DOWN, RIGHT, LEFT, ESC, F1-F10, BACKSPACE, DEL
RETURN, TAB, SPACE does nothing

Any idea?
A500 / A600 / A1200 / SAM440
WinUAE OS3.9 (AmiKit) / OS4.1FE (FlowerPot)
---
https://twitter.com/TarzinCDK

User avatar
airsoftsoftwair
Posts: 4246
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: Event Handler with some keys

Post by airsoftsoftwair »

RETURN and SPACE are passed as ASCII codes instead of strings for easier handling. So you need to something like:

Code: Select all

If msg.key = "\n" Then Print("RETURN")
If msg.key = " " Then Print("SPACE")
etc.

User avatar
Tarzin
Posts: 112
Joined: Mon Feb 15, 2010 11:46 am
Location: Dunkerque / FRANCE
Contact:

Re: Event Handler with some keys

Post by Tarzin »

Working fine, thanks!

May be this section in help should be updated?
%KEYDOWN

You should move this post in general programming instead of Windows IDE (I can do it myself, sorry)
A500 / A600 / A1200 / SAM440
WinUAE OS3.9 (AmiKit) / OS4.1FE (FlowerPot)
---
https://twitter.com/TarzinCDK

User avatar
airsoftsoftwair
Posts: 4246
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: Event Handler with some keys

Post by airsoftsoftwair »

%KEYDOWN is a very old Hollywood 1.x style function. You shouldn't use this in new scripts. It's only there for compatibility reasons.

Zandi
Posts: 4
Joined: Tue Aug 16, 2011 12:49 am

Re: Event Handler with some keys

Post by Zandi »

Hello all,
I have also a problem with the Event Handler

Code: Select all

    
   @SAMPLE 1, "Sound/01.wav"
@SAMPLE 2, "Sound/02.wav"

Function p_SwitchButton(msg)
	If msg.action = "OnKeyDown"
		 If (msg.key = "1") Or (msg.key = "2")
	 
			If dointerval = True Then ClearInterval(1)
			dointerval = False
			Switch msg.key
			
			Case "1"
				PlaySample(1) 
			Case "2"
				PlaySample(2) 

			EndSwitch
		EndIf					
	EndIf
EndFunction

InstallEventHandler({OnKeyDown = p_SwitchButton})

EscapeQuit(True)
Repeat
	WaitEvent
Forever
Why when I hold key 1 or 2 the sound loop and closes the program?
How can I set the program to play sound only when I hold the key?

Any suggestions?

User avatar
airsoftsoftwair
Posts: 4246
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: Event Handler with some keys

Post by airsoftsoftwair »

Try using "OnKeyUp" instead of "OnKeyDown". OnKeyDown will trigger multiple events while the key is down. OnKeyUp will trigger only once.

Zandi
Posts: 4
Joined: Tue Aug 16, 2011 12:49 am

Re: Event Handler with some keys

Post by Zandi »

Andreas wrote:Try using "OnKeyUp" instead of "OnKeyDown". OnKeyDown will trigger multiple events while the key is down. OnKeyUp will trigger only once.
I've tried with "OnKeyUp" but this is not a perfect solution for my program Harmonica(windows), Harmonica(AmigaOS4).
And still the combination of shift + (0-9) closes program when I hold the keys (only AmigaOS4).

User avatar
airsoftsoftwair
Posts: 4246
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: Event Handler with some keys

Post by airsoftsoftwair »

If you don't want to use OnKeyUp you have to implement a flag so that only the first OnKeyDown event calls PlaySample(). That's pretty easy.

I don't understand the issue that your program gets closed. Do you mean it exits completely without any error message whatsoever?

Zandi
Posts: 4
Joined: Tue Aug 16, 2011 12:49 am

Re: Event Handler with some keys

Post by Zandi »

Andreas wrote:If you don't want to use OnKeyUp you have to implement a flag so that only the first OnKeyDown event calls PlaySample(). That's pretty easy.
I don't know how to implement a flag I'm beginner if you could give me a example I will be very grateful for your help.
What happen after the implementation a flag? the previous playing sample will be disable or will play at the same time?
Andreas wrote: I don't understand the issue that your program gets closed. Do you mean it exits completely without any error message whatsoever?
I have always the same error message about problem with allocate free channels (multiple events).

jalih
Posts: 254
Joined: Fri Jun 18, 2010 8:08 pm
Location: Finland

Re: Event Handler with some keys

Post by jalih »

Zandi wrote: I don't know how to implement a flag I'm beginner if you could give me a example I will be very grateful for your help.
What happen after the implementation a flag? the previous playing sample will be disable or will play at the same time?
Not sure, if this is what you want but try this:

Code: Select all

@SAMPLE 1, "shoot.wav"

spacekey = False

Repeat
	If IsKeyDown("SPACE")
		If Not spacekey
			spacekey = True
			TextOut(#CENTER, #CENTER, "SPACE pressed")
			PlaySample(1, 0)
		EndIf
	Else
	 	spacekey = False
		Cls()
		StopSample(1)
	EndIf	
Forever

Post Reply