Windows项目c ++用键盘模拟钢琴键

时间:2014-05-20 15:00:46

标签: c++ key simulate

我需要用键盘模拟按下的钢琴键,但我无法让它工作。 当我创建窗口(执行我的代码)时,我可以播放声音,但是当我按任意键时却不能。如果我把它们放在开关(消息)之前,这些声音会自动播放。我做过一些明显错误的事吗?

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HMIDIOUT hMidiOut;
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;

// Open MIDI for output 
midiOutOpen(&hMidiOut, -1, 0, 0, 0); 
// Set instrument to 0 = Acoustic Grand Piano 
midiOutShortMsg(hMidiOut, DWORD(0x0C0 | 0 | (0 << 8) | (0 << 16))); 


switch (message)
{
case WM_KEYDOWN:
{
switch (wParam) {

case VK_LEFT:

    // Set instrument to 0 = Acoustic Grand Piano

    midiOutShortMsg (hMidiOut, DWORD(0x090 | 0 | (65<<8 ) | (64 << 16)));
    break;

    case 'S':
    midiOutShortMsg (hMidiOut, DWORD(0x090 | 0 | (62<<8 ) | (64 << 16)));   
    break;

    case 'D':
    midiOutShortMsg (hMidiOut, DWORD(0x090 | 0 | (64<<8 ) | (64 << 16)));
    break;

    case 'F':
    midiOutShortMsg (hMidiOut, DWORD(0x090 | 0 | (65<<8 ) | (64 << 16)));
    break;

    case 'G':
    midiOutShortMsg (hMidiOut, DWORD(0x090 | 0 | (67<<8 ) | (64 << 16)));
    break;

    case 'H':
    midiOutShortMsg (hMidiOut, DWORD(0x090 | 0 | (69<<8 ) | (64 << 16)));
    break;

    case 'J':
    midiOutShortMsg (hMidiOut, DWORD(0x090 | 0 | (71<<8 ) | (64 << 16)));
    break;

    case 'K':
    midiOutShortMsg (hMidiOut, DWORD(0x090 | 0 | (72<<8 ) | (64 << 16)));
    break;
}}

0 个答案:

没有答案