Lorsque j'appuie sur une touche du pad je souhaiterai connaitre l'identifiant de la touche.
Je souhaite l'utiliser pour déclencher un son depuis notre cartoucheur pour la webradio codé en purebasic. J'ai par exemple 16 sons. Appui sur la PAD1 -> lance le son 'trompette.wav', Appui sur le PAD2 -> coupe le micro, ...
J'ai cherché sur le forum anglais et j'ai trouvé le code suivant :
http://forums.purebasic.com/english/vie ... 914b75f3b7
Code : Tout sélectionner
EnableExplicit
;
Structure MIDIs
MIDIIn.I
MIDIOut.I
Stat.A
Dat1.A
Dat2.A
Ons.I[128] ; to keep Sounding Notes
Chstep.S
EndStructure
;
Define Ev
Global _Midi.Midis,_TG,_TGOns,_Quit
Global _CHROMATIC$ = "C C#D EbE F F#G AbA BbB C " ; mixed # b
;
Macro Mid2(Txt,Pos) : PeekS(@Txt+Pos-1,2) :EndMacro
;
Macro MIDI2N(Nt) : Mid2(_Chromatic$,Nt % 12*2+1):EndMacro; MIDINote name
;
Procedure$ SBIN(N,Le=32) : ProcedureReturn RSet(Bin(N),Le,"0") :EndProcedure
;
Procedure$ GetOns() ; ret Notes Played From midiKbd
Protected I,T$
For I=0 To 127
If _Midi\Ons[I]
T$+Midi2N(I)
EndIf
Next
ProcedureReturn T$
EndProcedure
;
Procedure$ Ons2Midiby()
Protected I,T$
For I=0 To 127
If _Midi\Ons[I]
T$+Chr(I)
EndIf
Next
ProcedureReturn T$
EndProcedure
;
Procedure MIDIInFull(hMidiIn, Msg, Instance, Dat1, Dat2) ; get MIDINotes
With _Midi
Protected *Mem=@\Ons,A,B
Select Msg
Case #MM_MIM_DATA
Select Dat1 & $FF
Case 144
\Stat=Dat1 >> 8 ;Note
\Dat1=Dat1 >> 16 ;Velocity
If \Dat1 : SetGadgetText(_TG,"Note On "+Str(\Stat)+" : "+"Vel "+Str(\Dat1))
\Ons[\Stat]=1
Else : SetGadgetText(_TG,"Note Off "+Str(\Stat))
\Ons[\Stat]=0
EndIf
SetGadgetText(_TGOns,Getons())
Case 128
\Stat=Dat1 >> 8 ;Note
\Dat1=Dat1 >> 16 ;Velocity
\Ons[\Stat]=0
SetGadgetText(_TG,"Note Off "+Str(\Stat)+" : Vel "+Str(\Dat1))
SetGadgetText(_TGOns,SBin(PeekL(*Mem+32),12)+Sbin(PeekL(*Mem),12))
EndSelect
EndSelect
EndWith
EndProcedure
;
Procedure MIDIinitFull(Instrument=0)
With _Midi
If midiInGetNumDevs_()
If midiInOpen_(@\MidiIN, 0, @MIDIInfull(), 0, #CALLBACK_FUNCTION) = #MMSYSERR_NOERROR
If midiInStart_(\MidiIN) <> #MMSYSERR_NOERROR
MessageRequester("Error","Can't start MIDI IN",0)
End
EndIf
Else : MessageRequester("Error","Can't open MIDI IN",0)
End
EndIf
midiOutOpen_(@\MidiOUT, 0, 0, 0, 0)
midiOutShortMsg_(\MidiOUT, 192 | Instrument<<8 ) ; set instrument
If \MidiIN And \MidiOUT
If Not midiConnect_(\MidiIN, \MidiOUT, 0)
SetGadgetText(_TG, "MIDI setting ready! Play MIDI Keyboard")
Else : MessageRequester("Error","Can't connect MIDI",0)
End
EndIf
EndIf
Else
MessageRequester("Error","No Midi devices found",0)
End
EndIf
EndWith
EndProcedure
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
OpenWindow(0,0,0,400,100,"MIDI Play",#PB_Window_SystemMenu|1)
SetWindowColor(0,$22)
Define Wi=WindowWidth(0), He=WindowHeight(0)
_TG=TextGadget(-1,10,10,Wi-20,24,"")
_TGOns=TextGadget(-1,10,40,Wi-20,24,"")
Define TBVol=TrackBarGadget(-1,10,70,Wi-20,24,0,$Ffff)
SetGadgetState(Tbvol,$FFFF)
MIDIinitFull()
With _Midi
midiOutSetVolume_(\MidiOUT,$FFFF)
Repeat
If GetAsyncKeyState_(27)&$8000 :_Quit=#True : EndIf
Ev=WaitWindowEvent()
If Ev=#PB_Event_Gadget
Select EventGadget()
Case TBVol : midiOutSetVolume_(\MidiOUT,GetGadgetState(Tbvol))
EndSelect
EndIf
Until Ev=#PB_Event_CloseWindow Or _Quit
midiDisconnect_(\MidiIN, \MidiOUT, 0)
While midiInClose_(\MidiIN) = #MIDIERR_STILLPLAYING : Wend
While midiOutClose_(\MidiOUT) = #MIDIERR_STILLPLAYING : Wend
midiOutClose_(\MidiOUT)
EndWith
Merci à vous pour vos idées et vos retours d'expérience !