Hammerspoon: bind key ignore some application

Created on 29 Jan 2019  路  3Comments  路  Source: Hammerspoon/hammerspoon

do some example like below:

local function pressFn(mods, oldKey, newKey)
    return function()
        if hs.application.frontmostApplication():name() == "Emacs"  then
            --  It does not work here
            --  there are recursive calls
            --  event bubbling ?
            -- hs.eventtap.keyStroke(mods, oldKey, 1000)
        else
            hs.eventtap.keyStroke({}, newKey, 1000)
        end
    end
end

local function remap(mods, key, pressFn)
    hs.hotkey.bind(mods, key, pressFn, nil, pressFn)
end


remap({'ctrl'}, 'h', pressFn({'ctrl'}, 'h', 'left'))

I hope it can ignore the Ctrl_h in Emacs application, how to implement this ?

Most helpful comment

I agree, something like this could be handy!

I was just looking for something like this and I was able to accomplish thing by disabling the binding when entering the application I wanted to ignore, and re-enabling it when leaving the application. I did this with hs.window.filter.

Something like this, with Xcode in my case:

yourBinding = hs.hotkey.bind(...)

local wf=hs.window.filter
xcodeWF = wf.new("Xcode")
xcodeWF:subscribe(wf.windowFocused, function()
  yourBinding:disable()
end):subscribe(wf.windowUnfocused, function()
  yourBinding:enable()
end)

Hope that helps!

All 3 comments

I agree, something like this could be handy!

I was just looking for something like this and I was able to accomplish thing by disabling the binding when entering the application I wanted to ignore, and re-enabling it when leaving the application. I did this with hs.window.filter.

Something like this, with Xcode in my case:

yourBinding = hs.hotkey.bind(...)

local wf=hs.window.filter
xcodeWF = wf.new("Xcode")
xcodeWF:subscribe(wf.windowFocused, function()
  yourBinding:disable()
end):subscribe(wf.windowUnfocused, function()
  yourBinding:enable()
end)

Hope that helps!

@jiahut I found a working solution here.

@zef Thanks for the tip. Helped me a lot!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

fent picture fent  路  3Comments

franzwr picture franzwr  路  3Comments

tomrbowden picture tomrbowden  路  3Comments

dasmurphy picture dasmurphy  路  4Comments

BigSully picture BigSully  路  3Comments