int Note_1=0x3c; // Note_1 存放低音1
int KeyToNote(Word &Key)
{ // 据键求音符
int Idx[]={0,2,4,5,7,9,11};
int Code[4]={'1','Q','A','Z'}; //每排首键
int CodeL[4]={12,12,11,10}; //每排键数
int ScanKey=MapVirtualKey(Key,0); //求Key扫描码
for (int i=0;i<4;i++)
{
int wz=ScanKey-MapVirtualKey(Code[i],0);
if (wz>=0 && wz<CodeL[i])
{
int Note=Note_1+12*i;
if (wz>=7) // 在下一个八度
Note+=Idx[wz-7]+12;
else
Note+=Idx[wz];
return(Note);
}
}
return(0);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormKeyDown(TObject *Sender, WORD &Key,
TShiftState Shift)
{
if (PlayStat)
if (Key_Note[Key]==0) // 连续按下时防止重复发音
{
int Note=KeyToNote(Key);
if (Note!=0)
{
if (Shift.Contains(ssShift))
Note++; //Shift 升半音
if (Shift.Contains(ssCtrl))
Note--; //Ctrl 降半音
Key_Note[Key]=Note;
PlayNote(Note); //发声
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormKeyUp(TObject *Sender, WORD &Key,
TShiftState Shift)
{
if (PlayStat)
{
int Note=Key_Note[Key];
if (Note!=0)
StopNote(Note); // 停止发声
Key_Note[Key]=0;
}
}
//---------------------------------------------------------------------------