【问题标题】:macOS Sierra: Emulate Mouse Down and UpmacOS Sierra:模拟鼠标向下和向上
【发布时间】:2017-12-25 11:57:39
【问题描述】:

更新 Sierra 后,我不得不发现 Karabiner 不再工作,它可以很好地模拟指针点击。

有没有机会通过 Swift 或 Apple Script 或 Obj.C 获得它?

背景:使用Karabiner(和seil)我可以将caps-lock + d 向下和向上绑定到鼠标向下和向上以及在触控板移动之间。我的触控板不再处理按键,但对于指针移动仍然可以正常工作。

【问题讨论】:

    标签: mouseevent macos-sierra emulation


    【解决方案1】:

    回答我自己,在hammerspoon 中破解它。让 chrome select 工作非常棘手,也许它可以节省一两个小时:

    1 ~/.hammerspoon $ cat init.lua
    
    
    
    --[[ Left Keyboard Mouse (alt-d)
    
    The main problem was to get chrome based apps select text when we drag via
    keyboard.
    
    You MUST return true always in the handle_drag otherwise it fails to select.
    FF works, terminal works, chrome apps (atom, ...) don't.
    
    But true is preventing further processing of the drag coords,
    hs.mouse.getAbsolutePosition remains constant while dragging (!)
    => i.e. we MUST calc them via DeltaX, DeltaY, see below.
    
    
    --]]
    
    hs.alert.show("Config loaded")
    
    -- all mechanics stolen from here:
    -- local vimouse = require('vimouse')
    -- vimouse('cmd', 'm')
    
    now = hs.timer.secondsSinceEpoch
    
    evt = hs.eventtap
    evte = evt.event
    evtypes = evte.types
    evp=evte.properties
    
    drag_last = now(); drag_intv = 0.01 -- we only synth drags from time to time
    
    mp = {['x']=0, ['y']=0} -- mouse point. coords and last posted event
    l = hs.logger.new('keybmouse', 'debug')
    dmp = hs.inspect
    
    -- The event tap. Started with the keyboard click:
    handled = {evtypes.mouseMoved, evtypes.keyUp }
    handle_drag = evt.new(handled, function(e)
    
        if e:getType() == evtypes.keyUp then
            handle_drag:stop()
            post_evt(2)
            return nil -- otherwise the up seems not processed by the OS
        end
    
        mp['x']  = mp['x'] + e:getProperty(evp.mouseEventDeltaX)
        mp['y']  = mp['y'] + e:getProperty(evp.mouseEventDeltaY)
    
        -- giving the key up a chance:
        if now() - drag_last > drag_intv then
            -- l.d('pos', mp.x, 'dx', dx)
            post_evt(6) -- that sometimes makes dx negative in the log above
            drag_last = now()
        end
        return true -- important
    end)
    
    function post_evt(mode)
        -- 1: down, 2: up, 6: drag
        if mode == 1 or mode == 2 then
            local p = hs.mouse.getAbsolutePosition()
            mp['x'] = p.x
            mp['y'] = p.y
        end
        local e = evte.newMouseEvent(mode, mp)
        if mode == 6 then cs = 0 else cs=1 end
        e:setProperty(evte.properties.mouseEventClickState, cs)
        e:post()
    end
    
    hs.hotkey.bind({"alt"}, "d",
      function(event)
        post_evt(1)
        handle_drag:start()
      end
    )
    

    alt 我通过 karabiner 元素映射到 capslock

    【讨论】:

    • 非常感谢!!!你救了我。我有带有 Karabiner-Elements 的 M1 MBP,即使使用最新版本(14.3)也会导致内核恐慌。所以我不得不卸载Karabiner并寻找替代品。我发现hammerspoon 是Karabiner 的最佳替代品,那么我应该将大写锁定键重新映射到鼠标左键。我发现您的代码运行良好。谢谢!
    • 很高兴听到,干杯!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-05
    • 1970-01-01
    • 1970-01-01
    • 2017-09-30
    • 2017-03-12
    相关资源
    最近更新 更多