首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >macOS塞拉:模拟鼠标向下和向上

macOS塞拉:模拟鼠标向下和向上
EN

Stack Overflow用户
提问于 2017-07-20 09:30:33
回答 1查看 238关注 0票数 1

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

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

背景:使用Karabiner(和seil),我可以将caps-lock + d向下和向上绑定到鼠标向下和向上,以及在触控板之间的运动被理解。我的触摸板不再处理按键,但仍然可以很好地处理指针移动。

EN

回答 1

Stack Overflow用户

发布于 2017-07-24 06:16:26

我自己回答,在hammerspoon上破解了。让chrome select运行起来相当棘手,也许它能为某些人节省一两个小时:

代码语言:javascript
复制
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 I通过karabiner元素映射到capslock

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45203657

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档