Hammerspoon / hammerspoon

Staggeringly powerful macOS desktop automation with Lua
http://www.hammerspoon.org
MIT License
12.09k stars 582 forks source link

EventTap seems to be sending the event to the wrong window #1334

Open zen0wu opened 7 years ago

zen0wu commented 7 years ago

It's a bit hard to describe, I'll try my best. Not sure if this is the right place to ask, because I don't know if it's a bug or I'm doing anything wrong with my config. So sorry in advance for spamming.

What I did is, I want CapsLock key to be a hybrid Ctrl/Function key on my new keyboard, for example:

My approach is to use an eventTap that's activated when Caps is pressed, and stopped when it's released, the event tap watches keydown event and translate it.

The reason I didn't use modal for it is that,

The only problem I've found is, Caps-IJKL now are my arrow keys, if I switch a window using Cmd-Tab, then I press Caps-I, it'll actually trigger a up in the previous window. I have to type something real in the new window to make it switch. Not sure if this is a bug, or it's something I'm doing wrong in the config.

Here's my config: https://gist.github.com/shivawu/a28ab3016956db601df0bba37b75546e

zen0wu commented 7 years ago

Anyone available to help?

cmsj commented 7 years ago

Rather than modify the event you receive in your eventtap callback, try creating a new hs.eventtap.event event and returning that in the table. I'm guessing here, but it's worth a shot.

zen0wu commented 7 years ago

@cmsj Thanks a lot for the suggestion! Unfortunately it doesn't seem to work

Here's the updated config

local anneEt = hs.eventtap.new({ hs.eventtap.event.types.keyDown }, function(event)
  local kc = event:getKeyCode()
  local mapped = capsMap[inverseKeycodes[kc]]

  if mapped ~= nil then
    key = event:getKeyCode()
    mods = event:getFlags()
    if mapped.key ~= nil then
      key = hs.keycodes.map[mapped.key]
    end
    if mapped.mods ~= nil then
      for _, m in pairs(mapped.mods) do
        mods[m] = true
      end
    end
    return true, {hs.eventtap.event.newKeyEvent(mods, key, true)}
  end

  return false
end)
cmsj commented 7 years ago

@shivawu same symptoms as before? :(

zen0wu commented 7 years ago

Yeah... Could you try the same config with "F13" replaced by things like "Caps", should work the same way, right?