C++ Win32 - 响应按键

C++ Win32- Responding to Key Presses

本文关键字:响应 Win32 C++      更新时间:2023-10-16

我正在尝试制作我模拟的 win32 应用程序,我按下钢琴上的 C 键,键盘编号 60,并使用以下调用:midiOutShortMsg (hMidiOut, DWORD (0x090 | 0 | (60 << 8) | (64 << 16)));

释放 60 号键时,请按以下调用:midiOutShortMsg (hMidiOut, DWORD (0x080 | 0 | (60 << 8) | (0 << 16)));

问题是,当我按下按钮时,我只听到一次声音。 当我运行程序时,只有一个案例有效。

怎么能做到只要我按下声音就会重复。 以及如何做一个长旋律播放。

case WM_KEYDOWN:
{
    switch (wParam) {
    case VK_LEFT:
        midiOutOpen(&hMidiOut, -1, 0, 0, 0);
        // Set instrument to 0 = Acoustic Grand Piano
        midiOutShortMsg(hMidiOut, DWORD(0x0C0 | 0 | (0 << 8) | (0 << 16)));
        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;

我想你可以存储键状态(也许使用布尔数组)并在消息处理循环中进行播放。