Hammerspoon: Add support for Force Touch event information and trackpad haptic feedback

Created on 26 Aug 2016  Β·  15Comments  Β·  Source: Hammerspoon/hammerspoon

As the title says, it would be awesome! If you could point me in the right direction or of you know if this is even possible, i could have a look and send a PR!

Made a test with a webview... ⚑
forcetouch

enhancement help wanted

All 15 comments

This is something I would like to have, but I've done nothing yet beyond looking at the docs. It's not immediately clear if we can arbitrarily read global force data, or if we have to be presenting UI to get the information.

There's NSHapticFeedbackManager for emitting the little buzzes, which might be fun to have, and for reading pressure information there's https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSEvent_Class/#//apple_ref/doc/uid/20000016-SW274 which stands a reasonable chance of working in hs.eventtap.

One thing to note is that some of this API is fairly new, and since we (in theory) support anything from 10.8 onwards, any new API needs to be guarded such that it will fail gracefully on older machines :)

Cool! I'll look into this when i got time, i think it could be used for some cool stuff πŸ˜‰

Take a look at the hs.eventtap.event file event.m... I don't have a machine new enough to have a force touch trackpad, (though I've been eyeing the external one for a while now), and at the time I last updated eventtap Travis hadn't been updated to support the 10.10.3 additions, but a starting point might be to uncomment the lines starting at 831 (https://github.com/Hammerspoon/hammerspoon/blob/master/extensions/eventtap/event.m#L831) and see what type of information you can glean from hs.eventtap.event:getProperty and hs.eventtap.event:getRawEventData.

@cmsj, since uncommenting these will just add some entries into the event types table and not add any actual code, is there any reason _not_ to go ahead and uncomment these for 0.9.47?

@asmagill happy to uncomment them, but I don't seem to be able to get any Force Touch info from them, all the pressure info I can dig out for a leftMouseDown event is just 0 or 1.

I was hoping that looking at the raw info might show more, but we probably also need to identify some new properties to add... this is another module that should probably get a good overhaul once we're more firmly into 10.12.

Any news on this? πŸ˜„

in for news!

I'll just describe my theoretical use case in case that is helpful at all.... I got excited about this for the possibility of a seamless way of opening URLs in alternate browsers.

I use Safari as my default browser, but I'm logged into all work accounts there. So I have the Safari Tech Preview as my personal browser to slightly separate logged in accounts, sessions, google tracking etc... It's awesome. For me, it's just the right amount of separation between accounts while still preserving unilateral iCloud sync. πŸ™ŒπŸΌ

I run into issues when I click a link anywhere and they are _all_ funneled into regular Safari. I do a LOT of CMD-C, click, CMD-T, CMD-V, enter. Being able to force click a link to open in Safari Tech Preview vs Safari would be πŸ’΅. That would mean it would have to override the default quick look behavior, or at least not get interrupted by it. Would love to help but I'm new to Hammerspoon as of like yesterday. Thanks!

I've tried to access the global pressure level but it doesn't seem to be working as documented by apple

NSEvent.addGlobalMonitorForEvents(matching: .pressure, handler: self.eventHandler)

if i change .pressure to .keyDown for example, it works.

However, better touch tool has managed to do it in some way (maybe private API?)

Some initial research and testing is leading me to believe that it will be relatively easy to detect gestures and force touch pressure changes that occur to our own views (i.e. hs.canvas objects, perhaps hs.webview (though it has its own default handlers for page zoom, etc. which might be affected), and perhaps the console itself), but it might not be possible to detect them in general or targeted at other applications. At least not without private API's (and I'm not aware of any at this time) or without a kernel extension which, as discussed in other issues by @cmsj, is beyond the scope of what we want to add to Hammerspoon at present.

I am working on a module which might be able to provide some basic haptic feedback support -- I don't regularly have access to a machine with a force touch trackpad, though I will this weekend, so hopefully I can have it available for testing then.

@asmagill did that module experiment lead to anything? I suspect it involves private APIs, so I'm going to close this issue for not really being possible to solve in HS. Please re-open if I'm wrong :)

@cmsj - @asmagill's hs._asm.undocumented.touchdevice extension is pretty awesome - I'd love to see this eventually make its way into the Hammerspoon core:

https://github.com/asmagill/hammerspoon_asm.undocumented/tree/f929ecb31eca7b3c917d6fe93aab306006a17705/touchdevice

The touchdevice module grants access to some pretty raw "pre-gesture" data... I don't know yet how or if we can encapsulate it at a higher level to encompass specific gestures as combinations of touch frames, but it does show promise for some interesting ideas -- detecting the number of touches, turning off force-touch trackpad "clicks" so it can be used more like a drawing tablet, things like that, simulating pressure on non force-touch trackpads by sensing touch size...

The gui toolkit module I'm working on to sort of mirror python's tkinter will be able to provide multi-level buttons -- it's not quite pressure sensitive (and it's limited to when you're clicking a specific button created by Hammerspoon rather then a global "reading") but it could be used to simulate something like the image above.

It won't be ready for a while yet, maybe 0.9.57 or 0.9.58, depending upon how much has to be fixed/changed with the 10.13 release.

Wow, that's cool @asmagill

Imagine if we could detect force press on the apps top-bar or when holding down a key and force-pressing anywhere in the window, then "snap" the window to sizes, providing haptic feedback on the "snap-points".

However, I don't know how bad/nice this would be to actually use πŸ˜„ but it's cool.

I don't think we will be able to grab the pressure from an arbitrary screen location (at least not in a way that would prevent it from being passed on through to the underlying application). What we may be able to do is create a button that, when clicked (and Hammerspoon is the active application) can detect pressure levels; some fancy trigger mechanism (holding or pressing a hotkey first) might allow for some automated switching back and forth, but capturing, identifying, and suppressing the actual events -- not having any real luck there so far.

I have done that, by popping up a webview (from hammerspoon), and using javascript (safari/wkwebview supports force touch) under the mouse, works OK, but can't really come up with a usecase for it πŸ˜„ so what you are talking about is possible with the current code.

And yeah I didn't think about blocking the action to the underlying app.. well ☺️ I feel like force touch is a bit over hyped (i hyped a lot for it when it came out) but there's almost no real use case for it.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

iliyang picture iliyang  Β·  4Comments

reestr picture reestr  Β·  3Comments

asmagill picture asmagill  Β·  4Comments

fent picture fent  Β·  3Comments

aaronjensen picture aaronjensen  Β·  3Comments