Old 04-14-2008, 08:10 AM   #1
sws
Code Monkey
 
sws's Avatar
 
Join Date: Sep 2007
Location: Madison, WI
Posts: 850
Default 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?
Code:
// 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.
sws is offline   Reply With Quote
Old 04-14-2008, 08:16 AM   #2
schwa
Administrator
 
schwa's Avatar
 
Join Date: Mar 2007
Location: NY
Posts: 9,384
Default

2 ought to work ... that's how my track notepad plugin shortcircuits the plugin chain keystroke eating process.
schwa is offline   Reply With Quote
Old 04-14-2008, 10:16 AM   #3
sws
Code Monkey
 
sws's Avatar
 
Join Date: Sep 2007
Location: Madison, WI
Posts: 850
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
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 02:22 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2017, vBulletin Solutions Inc.