Thread: Key mapping
View Single Post
Old 04-14-2008, 10:16 AM   #3
sws
Code Monkey
 
sws's Avatar
 
Join Date: Sep 2007
Location: Madison, WI
Posts: 857
Default

Thanks schwa, I got it to work. For anyone else looking to do this, you have to:

1) Account for shift/control/alt pressed/unpressed messages (256/257 for shift and control, 260/261 IIRC for Alt)

2) Make sure the time MSG->time of your "accelerator" message matches the time from GetTickCount() in hookCommand, so that you don't get ? as the keystroke when a user runs your command from the binding window. P.S. good luck finding it documented it anywhere that MSG->time is from GetTickCount().

My code below only cares about keys a-z (and shift A-Z), but here it is for reference:
Code:
int translateAccel(MSG *msg, accelerator_register_t *ctx)
{
    static bool bShift = false;

    if (msg->message == 256 && msg->wParam == 16)
        bShift = true;
    else if (msg->message == 257 && msg->wParam == 16)
        bShift = false;
    else if (msg->message == 256 && msg->wParam >= 0x41 && msg->wParam <= 0x5A)
    {
        g_dwLastKeyMsg = msg->time;
        g_cLastKey = (char)msg->wParam;
	if (!bShift)
            g_cLastKey += 0x20;
    }
    return 0;
} 

// Called "later" from REAPER:
bool hookCommandProc(int command, int flag)
{
    if (g_accel.accel.cmd && command == g_accel.accel.cmd)
    {
        if (GetTickCount() - g_dwLastKeyMsg > 10) // arbitrary 10ms - if key is too long ago it must have been from something else
            g_cLastKey = 0;
        // Do your thing here.
        return true;
    }
    return false;
}
sws is offline   Reply With Quote