Is there anyway to programmatically generate a force click?

I've trying to programatically generate a force click in a Cocoa application. Is there anyway to do this? I've been investigating whether or not it's possible to post one to the system using CGEventPost, but so far no luck. It seems Apple hasn't updated their API for creating events in a long time. The docs make no mention of trackpad events, momentum scrolling, or any kind of pressure-sensitive event. CGEventTypes.h does have constants for momentum scroll related event fields, but that's it.


I've tried recording all events passing through the system using an event tap and replaying them, changing only the events' timestamps as appropriate. Replaying mouse clicks works, and replaying multitouch gestures like a three finger swipe left or right works and does indeed cause the space to change, meaning it is possible to reproduce event types that don't have a formal API for creating them. In fact replaying any sort of event works... except for force clicks. Replaying that only causes a regular click. However I have a hard time believing Apple broke their own system-level event model just for force clicks.


Any ideas?

No, you cannot post force click events via CGEventPost.

Beyond the response above, I'm curious to know a bit more about what you're trying to accomplish.

I'm not at liberty to go into detail about what I'm working on, but the general idea is to try to make force clicks invocable through means other than using a force-touch trackpad.


I'm skeptical that there's no way to do it, as I've so far managed to reproduce every other kind of trackpad-specific event, and as near as I can tell none of them (with the exception of mouse clicks) are supposed to be possible to post using CGEventPost.

It sounds like you're misunderstanding "There's no documented API for that" for "not possible".


The nice people from Apple won't tell you about API that aren't documented, and your fellow developers all get to write "We told you so" when your application uses an undocumented API that breaks, stops working, or randomly crashes your application.


After all, there's probably at least one method involving a kernel debugger that would let you generate any event you could possibly want to, if you had access to all of the required information. But that's not going to be a documented, or supported, way of doing so.

Are you just trying to make the trackpad click or are you looking to trigger a full Force Click that travels through the event stream? If you're just trying to make the trackpad click, check out the NSHapticFeedbackPerformer and NSAlignmentFeedbackFilter classes, available in 10.11. You might also want to look into the NSPressureConfiguration API, available in 10.10.3.

I am attempting the same thing! I think is doable we need to figure our the right CGEventField to populate

Wanna compare cheat sheets?

You can reach me at g [at] astro-hq [dot] com

Hi,


I want to record what the user has clicked my aim is to find out the clicable action and its text on what the user has clicked. Can anyone guid me on how to proceed with this.

I might be misunderstanding what it is you are trying to accomplish but why not take a more sane approach and monitor mouse clicks/trackpad clicks, get the rect/point of the click, and then convert that from whatever it is your are displaying. If this is text and you are using a textView, there are ways to do it that are much easier.


This notification will let you know whenever there is a click in the textView:

-(void)textViewDidChangeSelection:(NSNotification *)notification {
// sent whenever a click in the textView or a change in selection range
}


get the range of text selected:

NSRange theRange = [[self firstTextView] selectedRange];


This will give you the location in the text of the insertion point (which is the location of the last mouse click since that is where you are putting the insertion point):

NSInteger myLocation = [[[[self firstTextView] selectedRanges] objectAtIndex:0] rangeValue].location;


Check out NSLayoutManager for ways to get the glyph for some text and then the rect of a glyph.


If you're dealing with images, it would probably be even easier--monitor the mouse clicks and get the rect/point of clicks. Check out the documentation for NSView.


My point is, I suggest you rethink how to accomplish what you want to do. There is most likely a way to do it with the functions and methods apple provides and documents although it might not seem apparent at first. As has been mentioned, fiddling with stuff that isn't documented is rife with perils.

Is there anyway to programmatically generate a force click?
 
 
Q