Page 1 of 2
Event Handler with some keys
Posted: Wed Jun 08, 2011 10:29 pm
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?
Re: Event Handler with some keys
Posted: Wed Jun 08, 2011 11:05 pm
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.
Re: Event Handler with some keys
Posted: Thu Jun 09, 2011 7:54 pm
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)
Re: Event Handler with some keys
Posted: Sat Jun 11, 2011 1:04 pm
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.
Re: Event Handler with some keys
Posted: Sat Aug 20, 2011 8:51 am
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?
Re: Event Handler with some keys
Posted: Sun Aug 21, 2011 2:23 pm
by airsoftsoftwair
Try using "OnKeyUp" instead of "OnKeyDown". OnKeyDown will trigger multiple events while the key is down. OnKeyUp will trigger only once.
Re: Event Handler with some keys
Posted: Mon Aug 22, 2011 1:14 am
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).
Re: Event Handler with some keys
Posted: Tue Aug 23, 2011 11:25 pm
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?
Re: Event Handler with some keys
Posted: Fri Sep 09, 2011 2:25 pm
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).
Re: Event Handler with some keys
Posted: Fri Sep 09, 2011 3:14 pm
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