pqrs-org / Karabiner-Elements

Karabiner-Elements is a powerful utility for keyboard customization on macOS Sierra (10.12) or later.
https://pqrs.org/osx/karabiner/
The Unlicense
18.91k stars 839 forks source link

Apple Spolight search is not recognized as frontmost application #3794

Open SamStuckey opened 6 months ago

SamStuckey commented 6 months ago

Description

Examples

Showing EventViewer not responding to Spotlight

Screenshot 2024-04-28 at 9 11 13 AM

Showing EventViewer tracking Finder as frontmost application before opening spotlight

Screenshot 2024-04-28 at 9 12 17 AM

Showing Event Viewer still tracking Finder as frontmost application with spotlight open again

Screenshot 2024-04-28 at 9 12 23 AM

To Reproduce

  1. Open EventViewer
  2. Open Spotlight with Cmd+Spacebar shortcut

My Goal

I want to map ctrl+n and ctrl+p to my up and down arrows inside of spotlight. So far the best I've come up with is a global mapping with an exception for my terminal programs (Already using those shortcuts for VIM stuff). This works most of the time, unless I open spotlight on top of a terminal window. Terminal (Iterm2, actually) stays active as my frontmost application, disabling my up / down keying within spotlight.

My current config to accomplish this goal

Again, this works unless I try and use the spotlight search on top of a terminal window (which I do often)

  {
    "description": "Use n & p as up and down arrow keys",
    "manipulators": [
      {
        "conditions": [
          {
            "bundle_identifiers": [
              "com.googlecode.iterm2",
              "com.apple.Terminal"
            ],
            "type": "frontmost_application_unless"
          }
        ],
        "type": "basic",
        "from": {
          "key_code": "n",
          "modifiers": {
            "mandatory": [
              "left_control"
            ]
          }
        },
        "to": [
          {
            "key_code": "down_arrow"
          }
        ]
      },
      {
        "conditions": [
          {
            "bundle_identifiers": [
              "com.googlecode.iterm2",
              "com.apple.Terminal"
            ],
            "type": "frontmost_application_unless"
          }
        ],
          "type": "basic",
          "from": {
            "key_code": "p",
            "modifiers": {
              "mandatory": [
                  "left_control"
              ]
            }
          },
          "to": [
            {
              "key_code": "up_arrow"
            }
          ]
      }
    ]
  },

Ideal Outcome

  {
    "description": "Use n & p as up and down arrow keys",
    "manipulators": [
      {
        "conditions": [
          {
            "bundle_identifiers": [
              "com.apple.Spotlight"
            ],
            "type": "frontmost_application_if"
          }
        ]
        ...
      }
    ]
  },
temp4422 commented 4 months ago

Same. Also this mentioned: https://github.com/pqrs-org/Karabiner-Elements/issues/1428 and https://github.com/pqrs-org/Karabiner-Elements/issues/1982

dzirtusss commented 3 months ago

I managed to mitigate this problem in a following way:

  1. mapped Cmd+space to set mode-spotlight var
  2. mapped Esc with mode-spotlight: on to a) deactivate var b) send 2X Esc
  3. mapped Enter with mode-spotlight: on to a) deactivate var b) send Enter

Which basically keeps spotlight state in that mode-spotlight var.

That overall works for me. Problem is 2x Esc as one escape may be not enough to close spotlight with results (it resets instead), but... var tracking is lost. So, sending 2x Esc to close spotlight always fully (not a big deal).

When it very rarely does not work, I just have to click more escapes to clean everything.

My use case is a bit different - I want to leave vim mode in one app, and make hjkl back to normal in spotlight. But I guess overall approach to your case may be same.

Some snippet if it may help:

{
            "conditions": [
                {
                    "bundle_identifiers": [
                        "^com\\.tinyspeck\\.slackmacgap$"
                    ],
                    "type": "frontmost_application_if"
                },
                {
                    "name": "slack-vi-mode",
                    "type": "variable_if",
                    "value": true
                },
                {
                    "name": "slack-vi-mode-spotlight",
                    "type": "variable_unless",
                    "value": true
                }
            ],
            "from": {
                "key_code": "spacebar",
                "modifiers": {
                    "mandatory": [
                        "command"
                    ]
                }
            },
            "to": [
                {
                    "set_variable": {
                        "name": "slack-vi-mode-spotlight",
                        "value": true
                    }
                },
                {
                    "key_code": "spacebar",
                    "modifiers": [
                        "command"
                    ]
                }
            ],
            "type": "basic"
        },
{
            "conditions": [
                {
                    "bundle_identifiers": [
                        "^com\\.tinyspeck\\.slackmacgap$"
                    ],
                    "type": "frontmost_application_if"
                },
                {
                    "name": "slack-vi-mode",
                    "type": "variable_if",
                    "value": true
                },
                {
                    "name": "slack-vi-mode-spotlight",
                    "type": "variable_if",
                    "value": true
                }
            ],
            "from": {
                "key_code": "escape"
            },
            "to": [
                {
                    "set_variable": {
                        "name": "slack-vi-mode-spotlight",
                        "value": false
                    }
                },
                {
                    "key_code": "escape"
                },
                {
                    "key_code": "escape"
                }
            ],
            "type": "basic"
        },
        {
            "conditions": [
                {
                    "bundle_identifiers": [
                        "^com\\.tinyspeck\\.slackmacgap$"
                    ],
                    "type": "frontmost_application_if"
                },
                {
                    "name": "slack-vi-mode",
                    "type": "variable_if",
                    "value": true
                },
                {
                    "name": "slack-vi-mode-spotlight",
                    "type": "variable_if",
                    "value": true
                }
            ],
            "from": {
                "key_code": "return_or_enter"
            },
            "to": [
                {
                    "set_variable": {
                        "name": "slack-vi-mode-spotlight",
                        "value": false
                    }
                },
                {
                    "key_code": "return_or_enter"
                }
            ],
            "type": "basic"
        },