Could you provide a formula for determining keyboard shortcuts values?

Hey,

it's complicated.

The basic modifiers can be generated using a function like this (quick & dirty untested):


function createBitmaskForShortcut(shortcut, differentiateLeftRight) {
  // modern flags (don't support left / right)
  const NSEventModifierFlagCapsLock = 1 << 16; // Set if Caps Lock key is pressed.
  const NSEventModifierFlagShift = 1 << 17; // Set if Shift key is pressed.
  const NSEventModifierFlagControl = 1 << 18; // Set if Control key is pressed.
  const NSEventModifierFlagOption = 1 << 19; // Set if Option or Alternate key is pressed.
  const NSEventModifierFlagCommand = 1 << 20; // Set if Command key is pressed.
  const NSEventModifierFlagNumericPad = 1 << 21; // Set if any key in the numeric keypad is pressed.
  const NSEventModifierFlagHelp = 1 << 22; // Set if the Help key is pressed.
  const NSEventModifierFlagFunction = 1 << 23; // Set if any function key is pressed.

  // nexstep flags (support left / right modifiers)
  const NX_DEVICELCTLKEYMASK = 0x00000001; // left ctrl
  const NX_DEVICELSHIFTKEYMASK = 0x00000002; //left shift
  const NX_DEVICERSHIFTKEYMASK = 0x00000004; // right shift
  const NX_DEVICELCMDKEYMASK = 0x00000008; // left cmd
  const NX_DEVICERCMDKEYMASK = 0x00000010; //right cmd
  const NX_DEVICELALTKEYMASK = 0x00000020; //left option
  const NX_DEVICERALTKEYMASK = 0x00000040; // right option
  const NX_DEVICE_ALPHASHIFT_STATELESS_MASK = 0x00000080;
  const NX_DEVICERCTLKEYMASK = 0x00002000; // right ctrl

  let bitmask = 0;
  if(differentiateLeftRight) {
    if(shortcut.indexOf('leftcmd') != -1 ) {
      bitmask |= NX_DEVICELCMDKEYMASK;
    }
    if(shortcut.indexOf('rightcmd') != -1 ) {
      bitmask |= NX_DEVICERCMDKEYMASK;

    }
    if(shortcut.indexOf('leftopt') != -1 ) {
      bitmask |= NX_DEVICELALTKEYMASK;

    }
    if(shortcut.indexOf('rightopt') != -1 ) {
      bitmask |= NX_DEVICERALTKEYMASK;

    }
    if(shortcut.indexOf('leftctrl') != -1 ) {
      bitmask |= NX_DEVICELCTLKEYMASK;

    }
    if(shortcut.indexOf('rightctrl') != -1 ) {
      bitmask |= NX_DEVICERCTLKEYMASK;

    }
    if(shortcut.indexOf('leftshift') != -1 ) {
      bitmask |= NX_DEVICELSHIFTKEYMASK;

    }
    if(shortcut.indexOf('rightshift') != -1 ) {
      bitmask |= NX_DEVICERSHIFTKEYMASK;

    }
  } else {
    if(shortcut.indexOf('cmd') != -1 ) {
      bitmask |= NSEventModifierFlagCommand;
    }
    if(shortcut.indexOf('ctrl') != -1 ) {
      bitmask |= NSEventModifierFlagControl;
    }
    if(shortcut.indexOf('opt') != -1 ) {
      bitmask |= NSEventModifierFlagOption;
    }
    if(shortcut.indexOf('shift') != -1 ) {
      bitmask |= NSEventModifierFlagShift;
    }
    if(shortcut.indexOf('function') != -1 ) {
      bitmask |= NSEventModifierFlagFunction;
    }
  }

  return bitmask;

}

Example usage:

createBitmaskForShortcut('optcmd');

The BTTAdditionalConfiguration is only used when left right differentiation is necessary and contains modifiers in an older Apple format.

Example usage:

createBitmaskForShortcut('leftopt', true);

The problem is that there is also more logic necessary to create the modifiers correctly for all types of keys and all edge cases. (e.g. arrow keys always need the function flag added ).

1 Like