Use a MIDI button to alternate 2 keystrokes

florian

2016-10-05 11:10:16

Hi, a user wants to toggle two keyboard shortcuts with each button press on a MIDI control surface. The first time it's pressed, generate CTRL+Q, the next time CTRL+W, then CTRL+Q again, and so on.

florian

2016-10-05 11:30:39

This is relatively easy with a global variable (let's say g1) which remembers the previous state. Note that if you want to map multiple MIDI buttons like that, make sure to use different global variables for each one.

Every time the MIDI button is pressed, the variable needs to be set to the new value. The naive way "if g0==0 then g0=1 / if g0==1 then g0=0" will not work, because the first statement will set g0 to 1, which will then be picked up in the second statement, where it would set g0 back to 0. So you need to use a helper variable, let's say a local variable pp:

Code: Select all

if g0==0 then pp=1
if g0==1 then pp=0
g0=pp
Note: would be nice to have an "else" statement here... will come in a future version of MT Pro.

A more straightforward way to alternate the g0 variable are these alternatives:
  1. Code: Select all

    g0=1-g0
  2. Code: Select all

    g0=g0^1
  3. Code: Select all

    g0=g0+1
    if g0>1 then g0=0
We'll use the third way, because with it you can easily expand this example to cycle any number of keystrokes (not only 2).

Then you need 2 different translators, each one for each keystroke to send (as of now -- MT Pro version 1.8.1 -- you can neither use variables in keystroke actions, nor define multiple outgoing actions in one translator).

A Rule is used to inspect the current value of g0 and abort the translator if it has the "other" value:

Code: Select all

if g0==0 then exit rules, skip Outgoing Action
Now putting it all together (assuming that the MIDI controller's button you want to map is a MIDI Note 36 -- use MIDI Capture to find out which one is yours):

Code: Select all

[x] Preset 0: Toggle Keystrokes
 Default MIDI IN ports:   MIDI Controller

[x] Translator 0.0: Keystroke 1
Incoming: Note On on ch. 1 with note:36 (0x24) and any velocity
Rules:
  g0=g0+1
  if g0>1 then g0=0
  if g0!=1 then exit rules, skip Outgoing Action
Outgoing: Text: Ctrl(Q)

[x] Translator 0.1: Keystroke 2
Incoming: Note On on ch. 1 with note:36 (0x24) and any velocity
Rules: if g0!=0 then exit rules, skip Outgoing Action
Outgoing: Text: Ctrl(W)
Enjoy! Attached is the project file. Please post here if you've created interesting presets with that!
Florian
Attachments
ToggleAlternatingKeystrokes.bmtp
(1.48 KiB) Downloaded 170 times