~habibalamin/Commandeer

01c961bfa021e6f0eb24c55637bbc00a74fb2f6b — حبيب الأمين 1 year, 6 months ago 575b5f7 + 05215ae
Merge branch 'fix-hotkey-with-command-only-treated-as-command-alone'
1 files changed, 27 insertions(+), 25 deletions(-)

M Commandeer/CommandPressToEscapePressTransformer.swift
M Commandeer/CommandPressToEscapePressTransformer.swift => Commandeer/CommandPressToEscapePressTransformer.swift +27 -25
@@ 9,18 9,30 @@
import Foundation
import AppKit

func keyEventCmdPressedAlone(_ event: CGEvent) -> Bool
func keyEventCmdDown(_ event: CGEvent) -> Bool
{
  let keycode = event.getIntegerValueField(CGEventField.keyboardEventKeycode)

  // 55 = L Command
  // 54 = R Command
  return
    Int32(event.flags.rawValue) ==
      NX_NONCOALSESCEDMASK | NX_COMMANDMASK | NX_DEVICELCMDKEYMASK ||
    Int32(event.flags.rawValue) ==
      NX_NONCOALSESCEDMASK | NX_COMMANDMASK | NX_DEVICERCMDKEYMASK
    (keycode == 55 &&
      Int32(event.flags.rawValue) ==
        NX_NONCOALSESCEDMASK | NX_COMMANDMASK | NX_DEVICELCMDKEYMASK) ||
    (keycode == 54 &&
      Int32(event.flags.rawValue) ==
        NX_NONCOALSESCEDMASK | NX_COMMANDMASK | NX_DEVICERCMDKEYMASK)
}

func keyEventNoFlagsPressed(_ event: CGEvent) -> Bool
func keyEventCmdUp(_ event: CGEvent) -> Bool
{
  return Int32(event.flags.rawValue) == NX_NONCOALSESCEDMASK // no flags
  let keycode = event.getIntegerValueField(CGEventField.keyboardEventKeycode)

  // 55 = L Command
  // 54 = R Command
  return
    (keycode == 55 && Int32(event.flags.rawValue) == NX_NONCOALSESCEDMASK) ||
    (keycode == 54 && Int32(event.flags.rawValue) == NX_NONCOALSESCEDMASK)
}

func commandPressToEscapePressTransformer(proxy: CGEventTapProxy,


@@ 32,17 44,14 @@ func commandPressToEscapePressTransformer(proxy: CGEventTapProxy,
  // `UnsafeMutableRawPointer` -> `UnsafeMutablePointer`
  let refcon = refcon?.bindMemory(to: CGEvent?.self, capacity: 1)

  if let currentKeyDown = refcon?.pointee {
    if (event.type != .flagsChanged) {
      if (keyEventCmdPressedAlone(currentKeyDown)) {
        refcon?.initialize(to: nil)
      }
    } else if (keyEventNoFlagsPressed(event) &&
      keyEventCmdPressedAlone(currentKeyDown))
    {
      refcon?.initialize(to: nil)
  // 55 = L Command
  // 54 = R Command

  if let savedEvent = refcon?.pointee {
    refcon?.initialize(to: nil)

      if (event.timestamp - currentKeyDown.timestamp <= 200_000_000) // ns
    if (keyEventCmdUp(event)) {
      if (event.timestamp - savedEvent.timestamp <= 200_000_000) // ns
      {
        let eventSource = CGEventSource(event: event)



@@ 60,15 69,8 @@ func commandPressToEscapePressTransformer(proxy: CGEventTapProxy,

        return nil
      }
    } else if (keyEventCmdPressedAlone(event) &&
      !keyEventNoFlagsPressed(currentKeyDown))
    {
      // Don't treat this as a true cmd-pressed-alone to be stored,
      // treat it as whatever's already been stored.
    } else {
      refcon?.initialize(to: event)
    }
  } else {
  } else if (keyEventCmdDown(event)) {
    refcon?.initialize(to: event)
  }