View Full Version : Key mapping
I'm trying to find a way to see what key/MIDI event occurred to call the command handler for a plugin. This is a little tricky because a commandid can be mapped to multiple keys/MIDI commands, etc, so it's not as easy as doing a reverse lookup into the accelerator table.
Justin, maybe one of these functions?// todo: kbd_translateAccelerator, kbd_formatKeyName, kbd_getCommandName, kbd_reprocessMenu
There's a couple hacked ways I could probably do it:
1) Check reaper-kb.ini. Only works if user remapped key.
2) Register "accelerator" and try to match keystrokes with later hookcommand calls.
schwa
04-14-2008, 08:16 AM
2 ought to work ... that's how my track notepad plugin shortcircuits the plugin chain keystroke eating process.
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:
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;
}
vBulletin® v3.8.4, Copyright ©2000-2010, Jelsoft Enterprises Ltd.